Random.RandomizeSeed Broken?

Related to my other post, I’m using the Random class. I think there is a bug (or at least incorrect documentation) with the Random.RandomizeSeed method. Take this code:

System.Random.Seed = 0
Var a As Double = System.Random.Number

Var b As Double = System.Random.Number

System.Random.Seed = 0
Var c As Double = System.Random.Number

Var d As Double = System.Random.Number

It outputs the the following values:

a: 0.80711944
b: 0.65977969
c: 0.80711944
d: 0.65977969

Values a and c make sense (the seed was reset to 0) but b and d should be different. The docs state:

Changes the random object’s seed value to start a new sequence of pseudo random numbers.

This implies that the seed assigned will be randomised (as the method name suggests). In actuality, it appears that the same seed is being selected.

It does look like a bug

I’d be tempted to use

System.Random.Seed = ticks

Using a fixed seed gives me predictable results when testing out the map generation algorithm.

It’s also good for unit testing.

i used it this way
Var r1 As New Random

System.DebugLog r1.InRange(1,100).ToString

Var r2 As New Random

System.DebugLog r2.InRange(1,100).ToString

1 Like

I am no expert but this is the result I thought you should get.
And the piece of documentation you quoted seems ok to me. The key word is “pseudo”.

The sequence of numbers is not random, but “pseudo” random. You don’t know what you will get, but the numbers are set with the seed.

That way you can get the same series of numbers using the same seed, which may be convenient for debugging (I am not saying this is the reason why it works this way, just that it can be advantageous some times).


A great thread I have used in the past for RND, RANDOM, etc is the following one: The limits of Rnd - General - Xojo Programming Forum
In your specific instance I think what you are getting is as expected. When you seed the class (at whatever instance you do it) the next call will use the same seed every time. The following paragraph I believe tries to explain this:

You might use this when you want to re-initialize a Random object but don’t want to create an entirely new one. If you are creating multiple sequences, this helps to make the sequences unrelated to one another.

In other words, the sequence after the call will always be the same (but you can keep making the seed call to generate new sequences). If you call the class without using the seed property/method you should get different results (i.e. not the same sequence every time you call it) - i.e. more so like the RND function.

The issue is not that the sequence is the same (that’s expected if the seed is the same). The issue is that I’m not convinced the Random class is actually picking a new seed when you call RandomizeSeed().

Then you are probably right and it is broken as previous versions worked as expected.

it is because RandomizeSeed use the Seed before (in this static class)
so you set seed =0 the RandomizeSeed get the same number.
try without

System.Random.Seed = 0
1 Like

So you provide a fixed seed and that works
I thought you were (and are) saying that RandomiseSeed doesnt pick a random starting point.
If that is the case as the bug isnt fixed, use Ticks to set a seed that you wouldnt be able to predict
The effect is pretty much the same?

The specific issue I have is that I have a loop that generates some terrain. During testing, I set the seed before the loop to a known number (0 in this case) for repeatability. I then call other functions later that I want to have a random (i.e. not known) seed.

I could use System.Microseconds or System.Ticks (that’s what I have settled on) but the docs suggest that Random.RandomizeSeed() should do what I want. In actuality, it doesn’t.

I think @MarkusR is correct. It’s applying a fixed increment after the seed before. The docs should be more clear.

For map generation, wouldn’t you want predictable results? So the same seed always generates the same result? This is how it’s done in every game I’ve played.

The game is in development at the moment so a lot of this is engine testing.

The map should be randomised (think Civilization map).

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.