Bug in Rnd()

Report any Hollywood bugs here
Post Reply
Bugala
Posts: 1428
Joined: Sun Feb 14, 2010 7:11 pm

Bug in Rnd()

Post by Bugala »

windows 11, Hollywood 11, seems to be having a problem with Rnd()-function. Havent noticed this before, since I didnt check its working before.

Today happened to be checking if my small code worked right, and noticed Random itself wasnt working properly.

Here is video showing the problem:
https://youtube.com/shorts/dD1xpWSUruk?feature=share

In this video I have one line of code:

Code: Select all

Debigprint(Rnd(13))
I keep continously clicking the "run" button to run the program, and as you can see, the result isnt really random at all, but it seems like there is some slow timer that keeps going down, and result is based upon what time it is in that slow timer.

As in, if I click slowly result will be in way of 9, 6, 3.

If I keep clicking fast the "run"-button, then result is 9, 9, 9, 8, 8, 7, 7, 7
plouf
Posts: 737
Joined: Sun Feb 04, 2018 11:51 pm
Location: Athens,Greece

Re: Bug in Rnd()

Post by plouf »

i think its known, that why RndStrong() has been created
Christos
Bugala
Posts: 1428
Joined: Sun Feb 14, 2010 7:11 pm

Re: Bug in Rnd()

Post by Bugala »

No, even Rnd() is less random than RndStrong(), Rnd() should still feel like random.

If you look at the video, the Rnd() is not random at all anymore in the test, but when doing it multiple times, I can predict its outcome.

Although, now that I tested:

Code: Select all

For n = 1 To 10
	DebugPrint(Rnd(10))
Next
It appears only the first Rnd(10) is predictable, rest of the Rnd(10) are random enough to be said to be random.

By otherwords, solution to the problem that I had, would have been to do Rnd() twice, and take only the next Rnd() result into account.
Flinx
Posts: 382
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Bug in Rnd()

Post by Flinx »

I don't think that's a bug.
Since the function uses an LCG, RndSeed() is executed at startup, and that uses the system time as the initial value.
So during normal program execution, there's no longer any dependence on time. It's only noticeable because the test program is restarted with each run.
Bugala
Posts: 1428
Joined: Sun Feb 14, 2010 7:11 pm

Re: Bug in Rnd()

Post by Bugala »

Okay, that makes sense, I guess it is intended behavior then.

I was having this raffle and made just a simple program to pick the raffle winner. Hence the whole program was practically just running Rnd() once, and when I tested that other parts of the program was right, by running it several times in a row, I noticed the lack of randomness in the result.

But anyway, in cases where the program's purpose is just to pick one random number, then RndStrong() can be used to avoid this problem.
Post Reply