Reset Random Numbers

General FreeBASIC programming questions.
Post Reply
Username
Posts: 30
Joined: Jun 22, 2020 5:56

Reset Random Numbers

Post by Username »

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.
SARG
Posts: 1877
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Reset Random Numbers

Post by SARG »

randomize timer
jevans4949
Posts: 1188
Joined: May 08, 2006 21:58
Location: Crewe, England

Re: Reset Random Numbers

Post by jevans4949 »

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
deltarho[1859]
Posts: 4693
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Reset Random Numbers

Post by deltarho[1859] »

One way:

Code: Select all

Dim As Double Seed = Timer
Randomize Seed
' < Some FB code >
' Reset to opening seed
Randomize Seed
' < More FB code >
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 = GetSeed
Username
Posts: 30
Joined: Jun 22, 2020 5:56

Re: Reset Random Numbers

Post by Username »

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.
deltarho[1859]
Posts: 4693
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Reset Random Numbers

Post by deltarho[1859] »

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.

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
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.
SARG
Posts: 1877
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Reset Random Numbers

Post by SARG »

Username wrote: Oct 09, 2023 4:57 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.
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
deltarho[1859]
Posts: 4693
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Reset Random Numbers

Post by deltarho[1859] »

@SARG

You have just repeated my first post. :!:
SARG
Posts: 1877
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Reset Random Numbers

Post by SARG »

deltarho[1859] wrote: Oct 09, 2023 8:31 @SARG

You have just repeated my first post. :!:
Sorry I forgot your post and I answered after the post of the original poster which seems not have read yours :D
deltarho[1859]
Posts: 4693
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Reset Random Numbers

Post by deltarho[1859] »

SARG wrote:Sorry I forgot your post ...
It is not even two days old yet. :roll:
which seems not have read yours
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. :o

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