Generate Random Numbers | Programming Basics

Many-a-times our coding projects require us to generate random numbers between an upper and lower limit. In this article, you will learn how to do the same in six most popular programming languages – C++, C#, Java, Python, JS and PHP. Let’s get started.

Jump To:

C++

A C++ program to generate 5 random numbers between 1 and 10.

#include <iostream>
#include <cstdlib>
#include <ctime>

int main() {
    srand(time(NULL));
    for(int i=0; i<5; i++) {
        std::cout << (rand() % 10) + 1 << "\n";
    }
}

srand() – This function, defined in “cstdlib”, is used to set the seed for the rand() function. Seeding is necessary to randomize the set of numbers we get as output. The srand() function takes an integer argument which is by default 1.

time() – This function, defined in “ctime”, is used to fetch the current time in seconds, calculated from 1st January of 1970. It returns an object of class “time_t”, which is used by srand() function as a unique seed.

rand() – This function returns a random number depending on the seed set by srand() function. If we had not used srand() before, the compiler will still run it with its default argument 1. In order to get numbers between 1 and 10, we perform modulus operation with 10 and add 1 to the result.

Note: When we perform modulus operation on a number by 10 it’ll return the remainder obtained by dividing the number by 10. For example, 6789 % 10 returns 9. You’ll also see that we add 1 to the result, by doing which we set the minimum number we get to always be 1.

C#

A C# program to generate 5 random numbers between 1 and 10.

using System;

public class Example {
   public static void Main() {
      Random random = new Random();
      for(int i=0; i<5; i++) {
         Console.WriteLine(random.Next(1, 10));
      }
   }
}

Random class – In C#, the Random class is available under the System namespace. It can be used to generate pseudo-random numbers in C#.

.Next() – This method, belonging to the object random of class Random returns a pseudo-random integer. It takes two arguments which serve as the minimum and maximum, between which a random number is to be returned.

Note: Apart from integers, you can also generate pseudo-random doubles and bytes. For more information, visit Microsoft.

Java

A Java program to generate 5 random numbers between 1 and 10.

public class Main
{
   public static void main(String[] args) {
      for(int i = 0; i < 5; i++) {
         System.out.println((int)(Math.random() * 10 + 1));
      }
   }
}

Math.random() – This method returns a random number between (double) 0.0 and (double) 0.9… We multiply the result by 10 to get random numbers between (double) 0.0 and (double) 10.999… So, we typecast it to integer to get our result. We add 1 to the final result to exclude 0.

Note: You can also use the java.util.Random class. For more information, visit Stack Overflow.

Python

A Python program to generate 5 random numbers between 1 and 10.

import random
for _ in range(5):
    print(random.randint(1,10))

random.randint() – We use the randint() function of the random module to generate random integers in Python.

JavaScript

A JS program to generate 5 random numbers between 1 and 10.

for(let i=0; i<5; i++) {
   console.log(Math.floor((Math.random() * 10) + 1));
}

Math.random() – We use the random() method of JavaScript Math class to obtain random numbers. We multiply the return value by 10 to limit results to 10.999…, add 1 to exclude 0 and typecast the double result to integer using Math.floor() method.

PHP

A PHP program to generate 5 random numbers between 1 and 10.

<?php
for($i=0; $i<5; $i++) {
   echo rand(1,10);
   echo " ";
}
?>

rand() – The rand() function can be used in PHP to generate random numbers. It accepts two arguments which serve as the minimum and maximum between which the random number is to be returned.

Please note, that all the methods of generating random numbers mentioned above generate pseudo-random numbers, which means they are fine for practical use but not random enough for use in high security conditions.

If you have doubts or want to point out errors, comment below.