How does Randomness work?

This might just be the stupidest question to ever be asked on this site, but I don’t know please help

I have a code that generates a random number between 0 and 1 using Random.new():NextInteger()

If its “0” it adds 1 to a variable called "A", if its “1” it adds 1 to a variable called "B"

Then I an event to print A and B

Here is the thing, I thought if there was a 50/50 chance, the 2 numbers would be extremely close, and although they are, they’re not as close as I thought they would be after a good amount of tests
image

Am I doing something wrong or Is that just how random stats work?

Please excuse my ignorance, I was never a math person.

1 Like

can you show the script real quick

image

where are you setting the value of the bool / what is it set to?

image

so you want it to random but extremely close, like 1 unit away?

random is pretty much what it says it is, the results will vary on, and it’s basically rng.

I don’t, I just thought math worked like that

1 Like

RNG isn’t anything like that, for example if somethings 50/50:

5 times: A
1 time: B

Even if it’s 50/50, it’s not bound to happen always.

Yes but i thought, the more samples you give it, the more accurate it will be, like instead of 2 samples, 1000 samples

It’s just RNG, always is random. Randomness won’t have favor towards another number cause it hasn’t been picked.

1 Like

Interesting… by the way, Should i use math.random or Random.new()

I’ve read alot and some people say math.random is faster, some other people say Random.new() is more random, and some other people say they’re the same

It really doesn’t matter, but I seen more people use math.random, but it’s all up to you.

1 Like

math.random and other random functions use an algorithm that kind of “fakes” randomness, that is why you can give it a seed and get the same numbers every time, because it’s not true randomness. However, don’t ask me how those algorithms work lol
The way these work might be the reason why they aren’t as close. Or it could just be normal, the difference isn’t huge, and the relative gap between the two seems to gets lower the higher the numbers are (A-B)/(The bigger one, maybe)

There’s this video from Tom Scott which shows a wall of lava lamps that are used to get true randomness, it’s quite interesting

Random.new() is a lot more feature rich, and the number size limit is also much higher (idk if the limit for math.random() is 32 bits numbers, but the limit isn’t too hard to reach)

math.random() is more convenient so that’s the one I use more often

Wow, that was actually a very interesting video u got me watching, thanks for the info.

I’ll keep that in mind, btw I have a question, If i was making a percentage possibiltiy system, Should I randomly generate numbers from 1 to 100, or 0 to 100? I see people do 0 to 100, but if you count 0 as a number that means it can generate 101 different numbers, wouldn’t that make it 101%?

If you do it from 1 to 100, then you cannot get under 1%, so probably from 0 to 100. Otherwise there’s also 0 to 99 I guess…
And yes 0 to 100 is 101 numbers, things can be weird sometimes, it’s likes rgb, you got values from 0 to 255, which is 256 numbers, but if you want to make a increment thing, you have to do jumps of 15 instead of 16.

1 Like

I would say either generate a random integer between 1 and 100 or a random float between 0 and 1. If an item’s chance is zero percent, then its logical for it to be impossible to get. If the chances as percentages are not necessarily integers, just choose a float instead of an integer.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.