Generating Random Numbers In Different Programming Languages
Random numbers or precisely in computer terms pseudo random number generators are extensively used in computing around the world today, which have great importance in the applications of gambling, statistics, computer simulation, cryptography, gaming, and areas where an arbitrary random value is required. A very simple example are the websites which send you a first time password when you sign up, these are random generated password.
Almost all the popular languages today have built-in PRNGs (Pseudo Random Number Generator), so let us look at how to generate random numbers in various popular languages.
In Ruby we would be using the built-in function rand()
The random number generator function are found in the Python module random, you can load it as and when required.
In C# we need to use the Random class to generate random numbers, it's as easy as the following code looks.
The Java class Random in the java.util package, it pretty easy to use the class, see the following code example.
PHP has an in-built random number generator function rand(), it is automatically seeded when called the first time. Additionally you can install the mt_rand module which is based on Mersenne Twister algorithm which is faster and more reliable.
Perl's PRNG is similar to PHP's, except the function rand() does not accept range, instead it accepts an optional max value.
Erlang provides uniform:random function to generate random numbers, and it works pretty much the same way as the functions we have reviewed so far in this article.
Re: Generating Random Numbers In Different Programming Languages
I would add the basic one. The one in C/C++ in GNU/Linux
printf("%d",rand()%10); //prints a random number between 0 and 10(both exclusive)
|All times are GMT +5.5. The time now is 18:27.|