Random number

Hey guys how do I display a random number every time a button is pressed

Yeah.I don’t know too

Get the value of this every time you detect a button press:

math.random(min,max)

http://docs.coronalabs.com/daily/api/

http://docs.coronalabs.com/daily/api/library/math/random.html

As far as display, do you mean print to the console or show in a text field on the screen?  It the later, create a text object and change the text value every time you get a new random number.

@roaminggamer here is my code



– main.lua


_local widget = require (“widget”)
local pricerandom = math.random(10000,100000)
    

local rectangle = display.newRect(160,200,275,400)
     

local buy = widget.newButton
{
    left = -45,
    top = 350,
    id = “buybtn”,
    label = “Buy”,
    
}

local newcard = widget.newButton
{

    left = 160,
    top = 350,
    id = “newcardbtn”,
    label = “New Card”,
    

}

local function onObjectTouch( event )
     if event.phase == “began” then
        print( "Touch event began on: " )
       
     end
     return true
 end
newcard:addEventListener( “touch”, onObjectTouch )_

         

I figured it out!! :slight_smile:

my Code Below



– main.lua


– code below is acutally from local score = require(“score”)
local widget = require (“widget”)

    

local rectangle = display.newRect(160,200,275,400)
 

local buy = widget.newButton
{
    left = -45,
    top = 350,
    id = “buybtn”,
    label = “Buy”,
    
}

local newcard = widget.newButton
{

    left = 160,
    top = 350,
    id = “newcardbtn”,
    label = “New Card”,
    

}

local function onObjectTouch( event )
     if event.phase == “began” then
        print( "Touch event began on: " )
        local price = display.newText(math.random(100,10000),50,50,native.systemFont,16)
          price:setTextColor(1,0,0)
     end
     return true
 end

           
newcard:addEventListener( “touch”, onObjectTouch )
 

@tpjacobson01,

The code you posted above will not work for what you want. Specifically, because you are creating the text object inside a touch function, every time you touch the button, you are just creating another text object on top of the first. I would suggest localizing your text object outside the touch function. Then inside the touch function do what Ed suggested, use “price.text”. The .text feature allows you to change the text of a text object. It’s convenient here because you don’t have to remove an object and spawn a new one every time you want a new number.

local widget = require ("widget") local rectangle = display.newRect(160,200,275,400) local price = display.newText(math.random(100,10000),50,50,native.systemFont,16) price:setTextColor(1,0,0) local buy = widget.newButton { left = -45, top = 350, id = "buybtn", label = "Buy", } local newcard = widget.newButton { left = 160, top = 350, id = "newcardbtn", label = "New Card", } local function onObjectTouch( event ) if event.phase == "began" then print( "Touch event began on: " ) price.text = math.random(100,10000) end return true end newcard:addEventListener( "touch", onObjectTouch )

Yeah.I don’t know too

Get the value of this every time you detect a button press:

math.random(min,max)

http://docs.coronalabs.com/daily/api/

http://docs.coronalabs.com/daily/api/library/math/random.html

As far as display, do you mean print to the console or show in a text field on the screen?  It the later, create a text object and change the text value every time you get a new random number.

@roaminggamer here is my code



– main.lua


_local widget = require (“widget”)
local pricerandom = math.random(10000,100000)
    

local rectangle = display.newRect(160,200,275,400)
     

local buy = widget.newButton
{
    left = -45,
    top = 350,
    id = “buybtn”,
    label = “Buy”,
    
}

local newcard = widget.newButton
{

    left = 160,
    top = 350,
    id = “newcardbtn”,
    label = “New Card”,
    

}

local function onObjectTouch( event )
     if event.phase == “began” then
        print( "Touch event began on: " )
       
     end
     return true
 end
newcard:addEventListener( “touch”, onObjectTouch )_

         

I figured it out!! :slight_smile:

my Code Below



– main.lua


– code below is acutally from local score = require(“score”)
local widget = require (“widget”)

    

local rectangle = display.newRect(160,200,275,400)
 

local buy = widget.newButton
{
    left = -45,
    top = 350,
    id = “buybtn”,
    label = “Buy”,
    
}

local newcard = widget.newButton
{

    left = 160,
    top = 350,
    id = “newcardbtn”,
    label = “New Card”,
    

}

local function onObjectTouch( event )
     if event.phase == “began” then
        print( "Touch event began on: " )
        local price = display.newText(math.random(100,10000),50,50,native.systemFont,16)
          price:setTextColor(1,0,0)
     end
     return true
 end

           
newcard:addEventListener( “touch”, onObjectTouch )
 

@tpjacobson01,

The code you posted above will not work for what you want. Specifically, because you are creating the text object inside a touch function, every time you touch the button, you are just creating another text object on top of the first. I would suggest localizing your text object outside the touch function. Then inside the touch function do what Ed suggested, use “price.text”. The .text feature allows you to change the text of a text object. It’s convenient here because you don’t have to remove an object and spawn a new one every time you want a new number.

local widget = require ("widget") local rectangle = display.newRect(160,200,275,400) local price = display.newText(math.random(100,10000),50,50,native.systemFont,16) price:setTextColor(1,0,0) local buy = widget.newButton { left = -45, top = 350, id = "buybtn", label = "Buy", } local newcard = widget.newButton { left = 160, top = 350, id = "newcardbtn", label = "New Card", } local function onObjectTouch( event ) if event.phase == "began" then print( "Touch event began on: " ) price.text = math.random(100,10000) end return true end newcard:addEventListener( "touch", onObjectTouch )