How does a computer program generate random numbers? Example: when you ask Siri to give you a random number between 1 to 10, how does it come up with that number?

754 viewsMathematicsOther

How does a computer program generate random numbers? Example: when you ask Siri to give you a random number between 1 to 10, how does it come up with that number?

In: Mathematics

18 Answers

Anonymous 0 Comments

Others have described the different way “random” numbers are generated.

I’ll add two things:

1) from a programmer’s perspective, most computer languages have a built-in function and/or set of libraries for generating random numbers. You may not know what words like “function” or “library” mean in this context, and you don’t have to. The important part is that, as a programmer, I don’t have to know all the mathematical details. The tools I use to create programs make that easy for me: within my code I can basically say “give me a number between 1 and 10” and the system does it. I realize this is kind of just pushing your question to a different level, the point is just that we have ways to make all this easy for the people who write computer programs. That comes with a cost, though. If there’s something wrong with this easy way of generating random numbers in a particular programming language, it can affect lots and lots of programs all at once. As a programmer, I’m depending on whoever created that random number system. If I’m writing a super critical system that handles lots of money or the nuclear launch codes or something (disclaimer: I have never worked on nuclear launch codes), I may need to be a lot more careful than just using the easy system. On the other hand, if I’m writing a solitaire game, the easy system is probably just fine.

2) it may seem at first glance like the “pseudorandom” systems that others have described, where you get “random but not really” numbers, would not be as good. And sure, there are cases where it might be really important for a piece of software to need “as random as you can get” numbers. But there are other cases where the “pseudorandom” behavior is actually more desirable.

As an example of why pseudorandom numbers might be useful, I once needed to implement a feature that presented a series of images to the user, who would then make certain determinations about the images. (This was an opt-in feature allowing users of open source data to voluntarily help improve that data in a a gamified fashion, not the annoying captcha things that ask you which pictures contain fire hydrants or anything like that.)

It wasn’t important for the feature that the images any particular person saw be truly random. It was just important that they didn’t all see the same set of images (because that helped us get through all the images faster, among other reasons).

So we intentionally used a pseudorandom number generator using a seed based on current time. This had a couple of benefits. First, it was easy to code and it was “random enough” for our use case. But more importantly, if a user reported an issue, they could send us the url they were using, which contained the seed value. Then by using that seed, we (the programmers) could easily see exactly the same set of images the user had seen, in exactly the same order. This was helpful in fixing problems.

There would have been other ways to handle that, of course. But in our case, “pseudorandom” was a great fit for our needs.

You are viewing 1 out of 18 answers, click here to view all answers.