Discuss Scratch

Chiroyce
Scratcher
1000+ posts

JavaScript + Scratch | Random Bias Detector

Is Scratch's (JavaScript's) random number generator biased? Let's find out!

Here's a project for testing - https://scratch.mit.edu/projects/541722488/
For ease of typing, we'll be referring to ‘random number generator’ as ‘RNG’, remember that!

Variable Meanings:

“Max RAND” - The maximum random number the RNG will generate.
“ODD” - Number of times the RNG generated an ODD number from ‘1’ to ‘Max RAND’.
“EVEN” - Number of times the RNG generated an EVEN number from ‘1’ to ‘Max RAND’
“Tries” - How many random numbers were generated from ‘1’ to ‘Max RAND’ (will be reset to 0 everytime you change the Max RAND var)
“Biased Towards” - Shows which type of number (Odd or Even) was generated more by the RNG, and how many more than the other.
“Random Number” the last number the RNG generated from ‘1’ to ‘Max RAND’.

Clear Bias:

Weirdly, when the RNG generates a random number from 1 to 1,000,000,000,000,000,000,000 (1 with 21 zeros), it generates an even number 99.999% of the time, which is very, very biased! A Max RAND equal to a quadrillion (1000000000000000) is mostly close enough to 50% odd and 50% even, but it does favour EVEN numbers by almost 500 values on average. (sometimes goes as high as 900 and sometimes as low as 200)

Pick random 1 to 10?:

A Max RAND equal to 10 is mostly close enough to 50.1% odd and 49.9% even, but it does favour ODD numbers, and increases the favour along with the number of tries!

100,000 tries - ODD is favoured by 440 values
200,000 tries - ODD is favoured by 720 values
700,000 tries - ODD is favoured by 1500 values
1,500,000 tries - ODD is favoured by 1745 values
(approximates) and goes on ….

At last, randomness!:

A Max RAND equal to 100 is mostly close enough to 49.9% odd and 50.08% even, but it does favour EVEN numbers, and DOESN'T increases the favour along with the number of tries! And, at 1.5 Million tries, ODD is favoured?! Weird right? Or maybe just that it's quite random

100,000 tries - EVEN is favoured by 180 values
200,000 tries - EVEN is favoured by 170 values
700,000 tries - EVEN is favoured by 102 values
1,500,000 tries - ODD is favoured by 713 values

Conclusion:

According to my testing, the chances of getting an odd random number from 1 to 1,000,000,000,000,000,000,000 (1 with 21 zeros) is 1/260,000 on average calculated from 17 odd numbers!!

So if you want to generate HUGE random numbers - there will be a bias towards even numbers, otherwise, there's a 50% chance for each number. Good.

Credits:
100% code by @Chiroyce
The block in the thumbnail was taken from scratchblocks.
Chiroyce
Scratcher
1000+ posts

JavaScript + Scratch | Random Bias Detector

With 6 million+ tries, we only have 27 odd numbers

Chiroyce
Scratcher
1000+ posts

JavaScript + Scratch | Random Bias Detector

Do browsers make a difference? I get nearly the same bias on Safari (macOS) and Firefox (macOS)

Safari —


Firefox —
gdpr70f61245d597c25631fbb669
Scratcher
100+ posts

JavaScript + Scratch | Random Bias Detector

I'd guess this is due to JavaScript precision issues but I'm not really a math expert.

And yeah, it might make a difference since it's up to the browser's developer for how they choose to program/implement JavaScript. However, I'd assume that all Chromium browsers would act the same, or at least within the margin of error. So try Chrome/Edge/Opera/etc.

It looks like Safari doesn't run on Chromium, which I didn't know until just now.
Chiroyce
Scratcher
1000+ posts

JavaScript + Scratch | Random Bias Detector

Naleksuh wrote:

It looks like Safari doesn't run on Chromium, which I didn't know until just now.
bruh lol - it uses WebKit

Naleksuh wrote:

I'd guess this is due to JavaScript precision issues but I'm not really a math expert.
true…
Flowermanvista
Scratcher
1000+ posts

JavaScript + Scratch | Random Bias Detector

Chiroyce wrote:

Naleksuh wrote:

I'd guess this is due to JavaScript precision issues but I'm not really a math expert.
true…
That's almost certainly what's going on here - your upper bound of 1,000,000,000,000,000,000,000 is well above a double float's make safe integer of 9,007,199,254,740,991, so it's not actually possible to represent odd numbers between 9,007,199,254,740,991 and 1,000,000,000,000,000,000,000 (which accounts for about 99.999% of the integers between 1 and 1,000,000,000,000,000,000,000).
Chiroyce
Scratcher
1000+ posts

JavaScript + Scratch | Random Bias Detector

Flowermanvista wrote:

it's not actually possible to represent odd numbers between 9,007,199,254,740,991 and 1,000,000,000,000,000,000,000
So then what about the 0.0004% Odd numbers?
Maximouse
Scratcher
1000+ posts

JavaScript + Scratch | Random Bias Detector

Chiroyce wrote:

Flowermanvista wrote:

it's not actually possible to represent odd numbers between 9,007,199,254,740,991 and 1,000,000,000,000,000,000,000
So then what about the 0.0004% Odd numbers?
They're all less than or equal to 9,007,199,254,740,991.
Chiroyce
Scratcher
1000+ posts

JavaScript + Scratch | Random Bias Detector

Maximouse wrote:

They're all less than or equal to 9,007,199,254,740,991.
Ohhh that makes sense!!
Maximouse
Scratcher
1000+ posts

JavaScript + Scratch | Random Bias Detector

Chiroyce wrote:

Do browsers make a difference?
Probably not much – they generate random numbers slightly differently, but they all use the same format to represent numbers.

Last edited by Maximouse (Sept. 29, 2021 15:28:11)

Chiroyce
Scratcher
1000+ posts

JavaScript + Scratch | Random Bias Detector

bump
ninjaMAR
Scratcher
1000+ posts

JavaScript + Scratch | Random Bias Detector

I made this project. I'm not sure if that is vaild
PkmnQ
Scratcher
1000+ posts

JavaScript + Scratch | Random Bias Detector

Try mod 3 to see if it's just a float issue or it's actually biased.
mybearworld
Scratcher
1000+ posts

JavaScript + Scratch | Random Bias Detector

It MIGHT be biased


uh… I'll try turbo mode

WHAT THE HECK-

Even with some nono word wizardy

Last edited by mybearworld (Jan. 8, 2022 04:38:49)

Chiroyce
Scratcher
1000+ posts

JavaScript + Scratch | Random Bias Detector

mybearworld wrote:

It MIGHT be biased
it is.

Flowermanvista wrote:

That's almost certainly what's going on here - your upper bound of 1,000,000,000,000,000,000,000 is well above a double float's make safe integer of 9,007,199,254,740,991, so it's not actually possible to represent odd numbers between 9,007,199,254,740,991 and 1,000,000,000,000,000,000,000 (which accounts for about 99.999% of the integers between 1 and 1,000,000,000,000,000,000,000).

Maximouse wrote:

Chiroyce wrote:

Flowermanvista wrote:

it's not actually possible to represent odd numbers between 9,007,199,254,740,991 and 1,000,000,000,000,000,000,000
So then what about the 0.0004% Odd numbers?
They're all less than or equal to 9,007,199,254,740,991.
mybearworld
Scratcher
1000+ posts

JavaScript + Scratch | Random Bias Detector

Chiroyce wrote:

mybearworld wrote:

It MIGHT be biased
it is.
I know. I was meaning to mark irony, because I didn't get a single odd number.
imfh
Scratcher
1000+ posts

JavaScript + Scratch | Random Bias Detector

Try setting MAX RAND to 9007199254740991.0, 62% odd, 37% even.
Chiroyce
Scratcher
1000+ posts

JavaScript + Scratch | Random Bias Detector

imfh wrote:

9007199254740991.0
hmm … interesting …

imfh
Scratcher
1000+ posts

JavaScript + Scratch | Random Bias Detector

Chiroyce wrote:

imfh wrote:

9007199254740991.0
hmm … interesting …

-image-
It looks like you can detect what browser someone is using with this. Chrome/Edge/Scratch Desktop all have ~62% odd, while Firefox has ~68% odd.

Last edited by imfh (Oct. 4, 2021 15:32:45)

mybearworld
Scratcher
1000+ posts

JavaScript + Scratch | Random Bias Detector

imfh wrote:

It looks like you can detect what browser someone is using with this. Chrome/Edge/Scratch Desktop all have ~62% odd, while Firefox has ~68% odd.
Interesting. I'll remix to do just that.

Powered by DjangoBB