help seeding Rnd

Monkey Forums/Monkey Beginners/help seeding Rnd

dubbsta(Posted 2017) [#1]
i dont know how to use seed the right way and dont really understand docs pls help


Pakz(Posted 2017) [#2]
Seed sets the output of the Rnd command.

"Seed 1" sets it to one particular stream of numbers.
"Seed GetDate[4]+GetDate[5]" Sets the output to a different stream based on the time of day.

Example:
Seed 1
then:
Rnd(1,10)
Output could be :
1
4
6
4

Seed 2
then Rnd(1,10)
Output could be
4
9
4
2

Set the seed on strategic locations in your code. Becourse if you set the seed it wil let rnd return the same random numbers. I mosly put it in the Setup of the code.

Does this makes sense?


dawlane(Posted 2017) [#3]
Seed is a global variable used by the random number generator algorithm as a starting point to generate the same set of pseudo-random numbers. This means that the numbers generated will be consistent with a degree of known probability. Generally if you don't set a seed value the last number generated by a random function becomes the new seed value. This means that there is no such thing a true random number as the generator always requires a starting point.
You should start here for an understanding of seed.


dubbsta(Posted 2017) [#4]
Thanks guys
@dawlane thanks for the link
@pakz a little, didn't know it could be used like that cool. How about seed milliseconds saw it somewhere is that a good method, not sure how or where to implement it. As I'm writing this I kind of remember, is it...
Something = millisecs
Seed something ??? And would it go in onupdate
And where would seed 1 go if used instead. Onupdate and since it's global I never have to reference it?


Gerry Quinn(Posted 2017) [#5]
Seed( Millisecs() ) at the start of your code is intended to cause the Rnd() values to be different every time, by seeding with a value corresponding to the length of time the program is running.

You'll find it doesn't actuallly work very well, for two reasons:

(1) Often, your program just started a fraction of a second ago, so the numbers are similar
(2) If the first few Rnd() values are important, they will often be the same even if the seed is slightly dfferent

To fix (1) you want a better value than Millisecs(). I use an old hacked version of diddy for the function RealMillisecs(), but that's because I've been too lazy to implement the proper solution which uses App.GetDate(). That will allow you to calculate the milliseconds since the year 2000 (say), which will be different every time the app runs.

To fix (2), call Rnd() a few times after seeding, and discard the results. So, during OnCreate():

Seed( MyRealMillisecs() )
Rnd()
Rnd()
Rnd()

...and you're good to go.


dubbsta(Posted 2017) [#6]
ok sounds good

problem:
oncreate
tried Seed(1), Seed() 1. Seed(Millisecs())
end

error
identifier Seed not found


Gerry Quinn(Posted 2017) [#7]
Sorry, my bad. I have a tendency to expect such things to be functions, especially if the name is capitalised. But Seed is actually a global int.

So it should be
:
Seed = Millisecs()

Generally with stuff like this, if it doesn't work, put the cursor on Seed and press F1. Monkey documentation isn't the greatest among languages, but it does exist :D


dubbsta(Posted 2017) [#8]
hehe yeah unfortunately that didn't help either, i need things broken down to the nano level then banged in my head :\ ....
ok so to be clear seed is the "variable" or handle and like you said seed = millisecs() or like pakz seed = 1, Seed=GetDate[4]+GetDate[5] and it goes in oncreate and never needs to be called
will try now

update: success!