As an engineer, we have to deal with Random numbers in a wide variety of applications, including games, simulations, and cryptography. In this tutorial, we will learn how to generate a random number in the top 10 programming languages: Python, Java, JavaScript, C++, C#, Go, PHP, Ruby, Swift, and Kotlin.
What is a Random Number?
A random number is a number that is generated without any predictable pattern. This means that it is impossible to predict what the next random number will be. Random numbers are used in a variety of ways such as in computer simulations and games to create a more realistic experience.
How to Generate a Random Number
Computers cannot actually generate a true random number. This is because they use certain algorithms and follow instructions. What they generate is a pseudo-random number. It is a number that appears to be random but produced by using a deterministic algorithm.
We know these algorithms as Pseudo-random number generators (a.k.a. PRNGs). They require a base value or a seed to produce a series of random numbers. Generally, we use the system time or a rolling value as the seed.
The different programming languages have their own way of supporting the generation of random numbers. Some use built-in functions and some offload the logic to 3rd-party libraries to do the needful.
Also Read: 1-10 Random Number Generator
1. Generating Random Numbers in Python
Python has a built-in module called random
that provides a variety of functions for generating random numbers. To generate a random integer between 0 and 9, we can use the randint()
function. The randint()
function takes two arguments: the lower bound and the upper bound. The lower bound is the smallest number that can be generated, and the upper bound is the largest number that can be generated.
i. Python code.
import random
# Generate a random integer between 0 and 9
random_number = random.randint(0, 9)
# Print the random number
print(random_number)
Next, let’s check out the code for random float numbers between 0 and 1 using Python random()
. This function returns a floating-point number between 0 (inclusive) and 1 (exclusive).
ii. Python code.
# Generate a float random value in the range of 0 and 1
random_number = random.random()
# Print the random number
print(random_number)
We can also use the random
module to generate random numbers from other distributions, such as the normal distribution and the Poisson distribution.
Also Check: Generate Random Numbers in Python
2. Generating Random Numbers in Java
Java also has a built-in class called Random
that can be used to generate random numbers. To generate a random integer between 0 and 9, we can use the nextInt()
method. The nextInt()
method takes one argument: the upper bound. The upper bound is the largest number that can be generated.
i. Java code.
import java.util.Random;
# Create a new Random object
Random random = new Random();
# Generate a random integer between 0 and 9
int randomNumber = random.nextInt(10);
# Print the random number
System.out.println(randomNumber);
We can also produce a random float between 0 and 1 in Java by using the nextDouble()
method. This method returns a float value between 0 (inclusive) and 1 (exclusive).
ii. Java code.
# Generate a float random value in the range of 0 and 1
double randomNumber = random.nextDouble();
# Print the random number
System.out.println(randomNumber);
We can also use the Random
class to generate random numbers from other distributions, such as the normal distribution and the Poisson distribution.
Must Read: 10 Ways to Generate Random Numbers in Java
3. Using JavaScript
JavaScript has a built-in function called Math.random()
that can be used to generate random numbers. The Math.random()
function returns a floating-point number between 0 (inclusive) and 1 (exclusive). To generate a random integer between 0 and 9, we can use the following code:
JavaScript code.
// Generate a random integer between 0 and 9
const randomNumber = Math.floor(Math.random() * 10);
// Print the random number
console.log(randomNumber);
We can also use the Math.random()
function to generate random numbers from other distributions, such as the normal distribution and the Poisson distribution. However, there are no built-in functions for these distributions, so we will need to use external libraries.
4. Generate a Random Number in C++
C++ has a standard library <random>
that provides a variety of functions for generating random numbers. To generate a random integer between 0 and 9, we can use the uniform_int_distribution<>
class and the mt19937
class. The mt19937
class is a PRNG that is used to
To complete the code for generating a random integer between 0 and 9 in C++, we need to instantiate the uniform_int_distribution<>
class and the mt19937
class, and then use the uniform_int_distribution<>
class to generate a random integer. The following code shows how to do this:
C++ code.
#include <random>
// Generate a random integer between 0 and 9
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dist(0, 9);
int randomNumber = dist(gen);
// Print the random number
std::cout << randomNumber << std::endl;
In the above code, the std::random_device
class is used to generate a random seed value. The std::mt19937
class is a PRNG that takes the seed from the device class. Finally, the uniform_ class generates random integers from a uniform distribution.
In the same fashion, in C++, we can use the uniform_real_distribution<>
class and the mt19937
class to generate random floats. The following code shows how to do this:
Also Read: Generate Random Images
C++ code.
#include <random>
// Generating random floats between 0 and 1
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> dist(0.0, 1.0);
double randomNumber = dist(gen);
// Print the random number
std::cout << randomNumber << std::endl;
The class std::uniform_real_distribution<>
gives us the ability to generate decimal numbers in a random fashion.
5. Using C#
C# provides a built-in class called Random
for generating random numbers. To generate a random integer between 1 and 100, you would use the following code:
C-sharp code.
using System; namespace RandomNumbers { class Program { static void Main(string[] args) { Random random = new Random(); int randomNumber = random.Next(1, 101); Console.WriteLine(randomNumber); } } }
Also Read: 50 C# Interview Coding Questions
6. PHP to Generate a Random Number
PHP provides a built-in function called mt_rand()
for generating random numbers. To generate a random integer between 1 and 100, you would use the following code:
PHP code.
<?php $randomNumber = mt_rand(1, 100); echo $randomNumber; ?>
Find Out: 15 PHP Interview Questions for 5+ Years of Experience
7. Swift
Swift provides a built-in class called Random
for generating random numbers. To generate a random integer between 1 and 100, you would use the following code:
Swift code.
import Foundation let randomNumber = Int.random(in: 1...100) print(randomNumber)
8. Kotlin
Kotlin provides a built-in class called Random
for generating random numbers. To generate a random integer between 1 and 100, you would use the following code:
Kotlin code.
import kotlin.random.Random val randomNumber = Random.nextInt(1, 101) println(randomNumber)
9. Go
Go provides a built-in package called math/rand
for generating random numbers. To generate a random integer between 1 and 100, you would use the following code:
Go code.
package main import ( "fmt" "math/rand" "time" ) func main() { rand.Seed(time.Now().UnixNano()) randomNumber := rand.Intn(100) + 1 fmt.Println(randomNumber) }
Also Check: Top 10 GoLang IDEs for Coding
10. Ruby
Ruby provides a built-in class called Random
for generating random numbers. To generate a random integer between 1 and 100, you would use the following code:
Ruby code.
require 'random' random_number = Random.rand(1..100) puts random_number
Also Check: How to Generate Random Emails
Let’s Sum Up
Finally, you have seen how to generate a random number in 10 different programming languages. Now, let’s sum it up in short by generating random numbers between 0-9.
Language | Code |
---|---|
Python | random.randint(0, 9) |
Java | new Random().nextInt(10) |
JavaScript | Math.floor(Math.random() * 10) |
C++ | std::uniform_int_distribution<>(0, 9)(std::mt19937()) |
C# | new Random().Next(10) |
Go | rand.Intn(10) |
PHP | rand(0, 9) |
Ruby | rand(10) |
Swift | Int.random(in: 0...9) |
Kotlin | Random().nextInt(10) |
Conclusion
Generating random numbers is a common task in programming. In this tutorial, we have learned how to generate random numbers in the top 10 programming languages. We have also learned about some of the different ways that random numbers can be used.
Random numbers can be used in a variety of applications. For example, they can be used to:
- Generate random numbers for games and simulations
- Create random passwords and other security tokens
- Shuffle lists and other collections
- Select random samples from large datasets
Don’t Miss: Top Programming Languages for Android in 2023