Random numbers and tapping events

Hi folks, I´m pretty new to corona and lua, although I had some experience with python and C++ programming.

I´m doing a kind of deck cards game and would like to generate a “true” random number with a seed value based on the X or Y position when a button is tapped on screen.

Does anybody knows if it´s possible to pass an X or Y value as a random seed?

Thank you!

https://docs.coronalabs.com/api/library/math/randomseed.html

To be more specific, X and Y values can be chained, added, multiplied or whatever before used with the function above.

Hope its what you are looking for

Hi @le_salvagnini,

If you’re creating a deck of cards type game, this tutorial might help you along:

https://docs.coronalabs.com/tutorial/data/shuffleTable/index.html

Best regards,

Brent

Tip: I don’t suggest re-seeding for each click.  It won’t necessarily give you an evenly distributed random sequence of numbers.  You may in fact get duplicates.

Note: Also, the random number generator is not deterministic across OSes because each OS supplies its own random library implementation.

If you need numbers to be deterministic, I may have something in SSK2  :

See: https://roaminggamer.github.io/RGDocs/pages/SSK2/external/#portable-random-community-code

OR: https://roaminggamer.github.io/RGDocs/pages/SSK2/external/#randomlua-love-community

If you simply want to ensure that you get random sequences on each run, seed once on startup from the clock.  See seeding link above from anaqim.

agreed… reseed in main.lua and pass os.time() as a parameter

First I would like to thank all of you for the help… 

I did my code and it´s working ALMOST fine… but now I´m fighting trying to clear the random numbers when I click the RIGHT mouse button, but it´s giving me some kind of error and the documentation doesn´t help too much.

Here is the main program:

I marked in red colour where I´m having difficulties.

What I´m doing wrong?

Thank you once more!

display.setStatusBar( display.HiddenStatusBar )

display.setDefault( “background”, 0.2, 0.2, 1.0 )

 

 

local PrgText = display.newText( “This program will generate two random numbers”, 500, 120, native.systemFont, 45 )

local TapText = display.newText( “Please click anywhere on screen”,500,240, native.systemFont, 40)

 

– Called when a mouse event has been received.

local function onMouseEvent( event )

if event.isPrimaryButtonDown then

–pickup screen coordinates at click point to make a more natural random seed

local X = (event.x)

local Y = (event.y)

–creates the random seed variable based on position and time

local seedXY = ((X/Y)*(os.time()) )

math.randomseed(seedXY)

–creates two variables to store random numbers from 1 to 100

local trnd1 = math.random(1,100)

local trnd2 = math.random(1,100)

–if the random values are equal, randomize second value again

if trnd2==trnd1 then

local trnd2 = math.random(1,100)

else

end

–Creates a display message on screen with two generated numbers

local mtext1= display.newText("The first generated number is: "…trnd1,500,400, native.systemFont, 40)

local mtext= display.newText("The second generated number is: "…trnd2,500,500, native.systemFont, 40)

 

–When RIGHT mouse button is clicked, clears the random values generated

elseif event.isSecondaryButtonDown then

mtext1:removeSelf()

mtext1=nil

mtext:removeSelf()

mtext=nil

 

end

end

– Add the mouse event listener.

Runtime:addEventListener( “mouse”, onMouseEvent )

It would be helpful if you posted the error… but it is most likely trying to remove an object that doesn’t exit yet

This is the error that I´m getting, when I click on the right mouse button to clear screen…

Corona Simulator Runtime Error

Attempt to index global ‘mtext1’ (a nil value)

file: main.lua

Line:33

stack traceback:

main.lua:33: in function ‘?’

?: in function <?:182>

your 2 variables are out of scope.  mtext1  and  mtext.   Just declare them outside of that if-elseif block.

when the section elseif of that block is executed it does not know those 2 variables as they are declared and defined in the if block above it.

try this…  

local PrgText = display.newText( "This program will generate two random numbers", 500, 120, native.systemFont, 45 ) local TapText = display.newText( "Please click anywhere on screen",500,240, native.systemFont, 40) local mtext1 local mtext -- Called when a mouse event has been received. local function onMouseEvent( event ) --if event.phase == "ended" then if event.isPrimaryButtonDown then --pickup screen coordinates at click point to make a more natural random seed local X = (event.x) local Y = (event.y) --creates the random seed variable based on position and time local seedXY = ((X/Y)\*(os.time()) ) math.randomseed(seedXY) --creates two variables to store random numbers from 1 to 100 local trnd1 = math.random(1,100) local trnd2 = math.random(1,100) --if the random values are equal, randomize second value again if trnd2==trnd1 then local trnd2 = math.random(1,100) else end --Creates a display message on screen with two generated numbers mtext1= display.newText("The first generated number is: "..trnd1,500,400, native.systemFont, 40) mtext= display.newText("The second generated number is: "..trnd2,500,500, native.systemFont, 40) --When RIGHT mouse button is clicked, clears the random values generated elseif event.isSecondaryButtonDown then print("BLAH") display.remove(mtext1) --mtext1:removeSelf() mtext1=nil display.remove(mtext) --mtext:removeSelf() mtext=nil end --end end -- Add the mouse event listener. Runtime:addEventListener( "mouse", onMouseEvent )

Good luck.

Bob

Bob, thank you for your suggestion, I´ve implemented your code improvements, but now, even my random numbers are not showing up… 

But the simulator are running without any errors.

le_

I did not check all your code or test to see what it was suppose to do… just fixed the error you had.

Not sure what you mean ‘random numbers are not showing’  can you give more detail?

Bob

Bob, while i was clicking with left mouse button on screen, two messages with random numbers were showing up…

After the code modifications, nothing happens now, even the two random numbers are not showing any more.  :unsure:

There is just a blue screen with the two main messages to click anywhere, and when I click nothing happens.

le_

I can only guess that you accidentally changed something else in the code. Because it works on my simulator.  If I press left mouse, 2 text images with random numbers appear, if I then click right mouse they disappear.

I am not sure if there is more code to your project then what you sent initially.  But in any case maybe start a new project, copy the code i sent earlier and paste it into your main.lua and run it and you will see it should run and do exactly what I think you expect it to do. So maybe there is some other code you have changed.   

If testing it in a new fresh project does not work, reply to this and post your failing code as it is now.  Make sure you press the   <>   symbol on the form’s tool bar, and paste the code in that window so it looks readable.

Bob

Thank you ! I found the mistake, I accidentally uncommented one line in the code  and this was generating the problem…

Now it´s working fine! Thank you all for the time, help and patience!

https://docs.coronalabs.com/api/library/math/randomseed.html

To be more specific, X and Y values can be chained, added, multiplied or whatever before used with the function above.

Hope its what you are looking for

Hi @le_salvagnini,

If you’re creating a deck of cards type game, this tutorial might help you along:

https://docs.coronalabs.com/tutorial/data/shuffleTable/index.html

Best regards,

Brent

Tip: I don’t suggest re-seeding for each click.  It won’t necessarily give you an evenly distributed random sequence of numbers.  You may in fact get duplicates.

Note: Also, the random number generator is not deterministic across OSes because each OS supplies its own random library implementation.

If you need numbers to be deterministic, I may have something in SSK2  :

See: https://roaminggamer.github.io/RGDocs/pages/SSK2/external/#portable-random-community-code

OR: https://roaminggamer.github.io/RGDocs/pages/SSK2/external/#randomlua-love-community

If you simply want to ensure that you get random sequences on each run, seed once on startup from the clock.  See seeding link above from anaqim.

agreed… reseed in main.lua and pass os.time() as a parameter