Reset Random Numbers
Reset Random Numbers
Hello.
When my program starts, and the Rnd() function is called, the values returned are always the same.
But how do I re-seed the random number generator to generate those same values again, as if the program started again?
I've tried different Randomize parameters, and none of them reset the values to how it is at the program start.
Is there a way to do it?
Thanks.
When my program starts, and the Rnd() function is called, the values returned are always the same.
But how do I re-seed the random number generator to generate those same values again, as if the program started again?
I've tried different Randomize parameters, and none of them reset the values to how it is at the program start.
Is there a way to do it?
Thanks.
Re: Reset Random Numbers
randomize timer
-
- Posts: 1188
- Joined: May 08, 2006 21:58
- Location: Crewe, England
Re: Reset Random Numbers
Randomize timer uses the current value of the timer to set the rnd function going.
Depends on how your chosen ransomizer works, but you should be able to generate the same numbers over and over by randomize with a defined value - either a constant or a value you key in.
Suggest you try it and see with a simple loop to call rnd a few times and print the results
Depends on how your chosen ransomizer works, but you should be able to generate the same numbers over and over by randomize with a defined value - either a constant or a value you key in.
Suggest you try it and see with a simple loop to call rnd a few times and print the results
-
- Posts: 4693
- Joined: Jan 02, 2017 0:34
- Location: UK
- Contact:
Re: Reset Random Numbers
One way:
However, I do not advocate using Timer. See here for why and an alternative approach.
If you use the alternative approach, then replace the opening statement above with:
Code: Select all
Dim As Double Seed = Timer
Randomize Seed
' < Some FB code >
' Reset to opening seed
Randomize Seed
' < More FB code >
If you use the alternative approach, then replace the opening statement above with:
Code: Select all
Dim As Double Seed = GetSeed
Re: Reset Random Numbers
I should add that I want to generate the default values from the start again.
Randomize Timer doesn't reset the values to the default.
Randomize Timer doesn't reset the values to the default.
-
- Posts: 4693
- Joined: Jan 02, 2017 0:34
- Location: UK
- Contact:
Re: Reset Random Numbers
The first statement of my code makes a copy of Timer via 'Seed = Timer'.
I have added some FB code as follows. Do not question the code – just run it.
Admittedly, the first block will be different each time the code is run, but the second block will be a repeat of the first block. I assume that is what you want.
If, for some strange reason, you want to use the algorithm's default seed, then just use Rnd as you were doing. When you want to repeat using the algorithm's default seed, then use 'Randomize 0'; but not with FB_RND_QB (4). The first Rnd value generated using the algorithm's default seed with FB_RND_MTWIST (3) is 0.3300995409954339. This is not mentioned in the manual. I won't go into details, but using a hard-wired seed is not a good idea – with the FB algorithms, some seeds are better than others. Using a seed of zero is the worst seed we can use – copying Timer is better. Copying GetSeed as mentioned in my first post is better still.
None of the above applies to the FreeBASIC algorithm FB_RND_REAL (5) because it is a cryptographic algorithm which does not use a seed.
I have added some FB code as follows. Do not question the code – just run it.
Code: Select all
Dim As Double Seed = Timer
Randomize Seed
For i As Ulong = 1 to 6
Print Rnd
Next
Print
Print "Reset seed to the opening seed"
Print
Randomize Seed
For i As Ulong = 1 to 6
Print Rnd
Next
Sleep
If, for some strange reason, you want to use the algorithm's default seed, then just use Rnd as you were doing. When you want to repeat using the algorithm's default seed, then use 'Randomize 0'; but not with FB_RND_QB (4). The first Rnd value generated using the algorithm's default seed with FB_RND_MTWIST (3) is 0.3300995409954339. This is not mentioned in the manual. I won't go into details, but using a hard-wired seed is not a good idea – with the FB algorithms, some seeds are better than others. Using a seed of zero is the worst seed we can use – copying Timer is better. Copying GetSeed as mentioned in my first post is better still.
None of the above applies to the FreeBASIC algorithm FB_RND_REAL (5) because it is a cryptographic algorithm which does not use a seed.
Re: Reset Random Numbers
Just keep the timer value in a DOUBLE variable and use it again when you want.....
Code: Select all
dim as double mystart_timer=timer
randomize mystart_timer
......
and later
randomize mystart_timer
-
- Posts: 4693
- Joined: Jan 02, 2017 0:34
- Location: UK
- Contact:
Re: Reset Random Numbers
@SARG
You have just repeated my first post.
You have just repeated my first post.

Re: Reset Random Numbers
Sorry I forgot your post and I answered after the post of the original poster which seems not have read yours

-
- Posts: 4693
- Joined: Jan 02, 2017 0:34
- Location: UK
- Contact:
Re: Reset Random Numbers
It is not even two days old yet.SARG wrote:Sorry I forgot your post ...

Suggesting Username's second post is referring to your first post. His second post is 33 hours after my first post and he didn't read it? Perhaps I'm on his foes list.which seems not have read yours

This forum is getting worse. I am getting so close to giving up on it.