Generating Random Numbers In Different Programming Languages

Discussion in 'Programming' started by pradeep, Sep 19, 2012.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    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.

    Ruby



    In Ruby we would be using the built-in function rand()

    Code:
    #!/usr/bin/ruby
    
    print rand()
    print "\n"
    print rand(11)
    print "\n"
    print (rand() * 10000).to_i()
    
    Output:
    Code:
    [pradeep@home-desktop test]# ruby test.rb
    0.669492104182758
    8
    6275
    

    Python



    The random number generator function are found in the Python module random, you can load it as and when required.

    Code:
    #!/usr/bin/python
    
    import random
    
    print random.randint(0,9)
    print random.random()
    
    Output:
    Code:
    [pradeep@home-desktop test]# python test.py
    4
    0.339478502497
    

    C#



    In C# we need to use the Random class to generate random numbers, it's as easy as the following code looks.

    Code:
    Random random = new Random();
    
    // now let's print a random number from 0 to 9
    Console.WriteLine(random.Next(0, 9));
    

    Java



    The Java class Random in the java.util package, it pretty easy to use the class, see the following code example.

    Code:
    import java.util.Random;
    
    Random generator = new Random();
    
    int r1 = generator.nextInt();
    double r2 = generator.nextDouble();
    

    PHP



    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.

    PHP:
    print rand() . "\n";
    print 
    rand() . "\n";

    print 
    rand(440). "\n";

    // Generate better random numbers with mr_rand
    print mt_rand() . "\n";
    print 
    mt_rand() . "\n";

    print 
    mt_rand(880);

    Perl



    Perl's PRNG is similar to PHP's, except the function rand() does not accept range, instead it accepts an optional max value.

    Code:
    print rand() . "\n";
    print rand(10) . "\n"; # returns a number between 0 & 10
    

    Erlang



    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.

    Code:
    % generates a number between 0.0 to 1.1
    random:uniform()
    % generates a number between 0 to 10
    random:uniform(10)
    
     
  2. smp

    smp New Member

    Joined:
    Jul 8, 2012
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    I would add the basic one. The one in C/C++ in GNU/Linux

    main()
    {
    srand(time(NULL));
    printf("%d",rand()%10); //prints a random number between 0 and 10(both exclusive)
    }

    Turbo/Borland C++

    main()
    {
    randomize();
    cout<<random(10);
    }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice