I'm not very sure how exactly a fractal number is stored nor how random function works in mordern programming languages.

But I am curious, will random function ever hits a hard coded decimal number?

I know mathematically it is impossible, but how about in computer science?

Such as the following javascript code, will it ever hit the number and stop?

// this can be any number
const rnd = 0.123456;

while (true) {
    if(rnd === Math.random()) {

Also I ran that code for 2 hours and it didn't stop

If decimal points are store in 64 bit, does that mean the chance to hit any decimal point is 1/2^64?

If it can hit, is 0.000123456 harder to hit than 0.01?


Your random number generator might not be able to produce all numbers. Without looking at Math.Random, if it returned an integer then it would never match 0.123456. If it returned a double precision floating point number 0 <= rnd < 1, it might produce a 53 bit integer from 0 to 2^53-1, and multiply by 2^-53. In this case it is quite possible that 0.123456 will never be hit.

But even if 0.123456 would be hit eventually, that random number generator would produce 2^53 possible values. On average it needs to produce 2^52 values before a match. That isn't going to happen within two hours. Assuming 10ns per number, 10ns * 2^52 is about 45 million seconds, or 521 days.


Well, it technically is possible (with a small caveat). This is due to the fact a computer has finite memory, and hence cannot represent all possible numbers, but rather only a very very small fraction of them (and thus the probability to "hit" some particular number it can represent is not 0). However, it still depends on the random function's behavior. For example (an example I totally made up, just for the point), if the random function will always return a value with only 4 digits after the decimal dot, then it can't return 0.123456. This can happen for a lot of reasons, not only if it returns a value with only 4 digits after the decimal point (which was a made up restriction).

So it is possible to hit numbers like that, maybe not 0.123456 specifically, but some other numbers.

Anyways, the computer can still represent a lot of numbers. Depending on your computer, it can be around $2^{32}$ different numbers, hence, if you try to actually run this code on your computer, it could take a lot of time and you might even start to think it will never finish...

Anyways, the probability to hit a certain number should depend on the randomized algorithm you are using (those are called pseudo-random number generators). For example, if you are using an algorithm that will generate uniformly numbers, then the probability should be around the same. But if you are using a different probability distribution (like a normal distribution), then the probabilities will be different.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.