Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers AppGameKit Corner / Random Number info

Author
Message
RickV
TGC Development Director
24
Years of Service
User Offline
Joined: 27th Apr 2000
Location: United Kingdom
Posted: 10th Oct 2017 12:55
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
Kevin Cross
21
Years of Service
User Offline
Joined: 15th Nov 2003
Location: London, UK
Posted: 10th Oct 2017 15:54 Edited at: 10th Oct 2017 16:02
But do you need to do this?: SetRandomSeed ( GetUnixTime ( ) )

Isn't this set this way by default? The help pages say that the seed is set from the time the apps are launched by default, without needing to set it with SetRandomSeed.

I get a different random sequence each time the app starts without needing to use this: SetRandomSeed ( GetUnixTime ( ) )

https://www.appgamekit.com/documentation/Reference/Core/SetRandomSeed.htm

Sets the seed for the random number generator. Two AppGameKit applications using the same seed value will generate the same sequence of random numbers. By default the seed is set to the current time on startup so that each run of the application will generate a different sequence of numbers.
CJB
Valued Member
20
Years of Service
User Offline
Joined: 10th Feb 2004
Location: Essex, UK
Posted: 10th Oct 2017 17:31
Confirmed. Tested both Random and Random2. Both offer a different sequence of random numbers when starting your app. Sounds like the user may have got something else wrong in their code. Did they post a snippet of what they were doing?
PSY
Developer
8
Years of Service
User Offline
Joined: 3rd Jul 2016
Location: Laniakea Supercluster
Posted: 11th Oct 2017 06:40
It's not about the code.

Random and Random2 will generate different numbers each time, even without SetRandomSeed ( GetUnixTime ( ) )

But only if you are NOT BROADCASTING the app.
Broadcasting the app without using SetRandomSeed ( GetUnixTime ( ) ) or any other seed that is not fixed will cause IDENTICAL NUMBERS EVERY TIME!

I guess when you broadcast an app, the command that is automatically called in the background and sets a time-based seed is buggy somehow. Maybe it's called when the time for the broadcast app hasn't been established yet and is still 00:00...


PSY
PSY LABS Games
Coders don't die, they just gosub without return

Login to post a reply

Server time is: 2024-11-24 12:27:31
Your offset time is: 2024-11-24 12:27:31