Hi,
This is to help someone from YouTube who asked about generating random numbers:
The user question was;
Quote: "I'm having some problems with the AppGameKit broadcasting app donwloaded from the playstore, when i broadcast the program the first time after launching the broadcasting app the resolution seems not quite right, instead, everytime after the first one give a problem with the "Random" command, instead of giving me a random number from 1 to 2, he give me always the number 2, i don't know what to do, please, help me, thanks.
"
This happens because the random command bases the numbers it generates off a seed. To solve this you will need to use the command SetRandomSeed, passing in a new seed number. Here's an example -
SetRandomSeed ( 10 )
numbers as integer [ 5 ]
for i = 1 to 5
numbers [ i ] = random ( 1, 5 )
next i
do
for i = 1 to 5
print ( numbers [ i ] )
next i
sync ( )
loop
This will generate the same sequence of numbers each time you run it.
Rather than having a fixed seed it's better to have one that changes each time the program runs. One option is to use the command GetUnixTime as your seed. Here's the above code with a minor change -
SetRandomSeed ( GetUnixTime ( ) )
numbers as integer [ 5 ]
for i = 1 to 5
numbers [ i ] = random ( 1, 5 )
next i
do
for i = 1 to 5
print ( numbers [ i ] )
next i
sync ( )
loop
Each time you run this you will get a different set of numbers displayed on screen.
Development Director
TGC Team