Discuss Scratch

ViAik
Scratcher
2 posts

Sequence of random numbers starts repeating after a certain time.

Шindows 10, Chrome 60.0.3112.90, Flash 26.0.

This isn't really a bug that should affect many people when making actual apps, but here it is, and I found it very interesting.
Summary: When you generate a large number of random numbers consecutively and put them in a set, after a certain time the numbers will stop being random and start repeating. The exact point at which this happens depends on the range of the random numbers.

Full version: I found this bug by accident while fidgeting around with random numbers. I was trying to find patterns in a sequence of randomly generated numbers to see if Scratch has any bias in terms of that, and eventually found that after a while the numbers start to repeat. I then made a new project in which I used code to find more info about this (link further down).
Here's how this works. I have a Forever script in which I generate a random number between, say, 1 and 8. Every step I add the generated number into variable “long_var”, next to the previous one (increasing the variable's value's length by one). I also write the number into a new item of the list “main”. That way, there is a sequence of random numbers written into each one. When the variable gets to 10238 characters/the list gets to 10238 items (I had to use Turbo mode to get there), the numbers start being the same - each item after this is the exact same value.
I used a program to find the exact point at which this happens given upper limit of random numbers using code. For example, for 1-digit numbers it's 10238, for 10 it's 9342, 11 = 8783, 12 = 8278, 20 = 6710, 32 = 6033, 100 = 5365, 500 = 3665 and so on. As you can see, the larger the range, the sooner repetition starts. I'm not a professional backend developer, so I have no idea why exactly this happens - maybe there is an explanation.
As said before, my program finds the spots at which repetition starts given upper limit of randomization. It graphs these values with an x increment of 10. (x = upper bound of random numbers, y = the point at which numbers start repeating) The result looks like this:

(Scratch smudges the points a bit, but you can see the shape of the curve. The tick marks are put every time x increases by 1000. The curve breaks at values 100, 1000, and slightly at 10,000).
Here's a link to this program.
Turbo mode doesn't affect this. I left the program running overnight with Turbo off and got the same results.
Hello ¯\_(ツ)_/¯

Last edited by ViAik (Aug. 7, 2017 18:41:18)

LionHeart70
Scratcher
1000+ posts

Sequence of random numbers starts repeating after a certain time.

Hmm..
I think it might have to do with how long it's running.
kenny2scratch
Scratcher
500+ posts

Sequence of random numbers starts repeating after a certain time.

That’s quite interesting!

If I were to hazard a guess at what’s going on, I’d say the seed ran out - but isn’t the seed generated with every new random number?
ViAik
Scratcher
2 posts

Sequence of random numbers starts repeating after a certain time.

kenny2scratch wrote:

That’s quite interesting!

If I were to hazard a guess at what’s going on, I’d say the seed ran out - but isn’t the seed generated with every new random number?
That's actually a great idea. I would explain why numbers start repeating sooner when their range is higher - the seed runs out sooner. I'm not sure about it being generated every new random number, though. In fact, I have no idea about any of the technical mechanisms behind Scratch

Last edited by ViAik (Aug. 10, 2017 19:46:25)

Powered by DjangoBB