A.I, Data and Software Engineering

Random seed

R

In this post, I have listed several methods to generate a random number. However, sometimes you can see people using a random seed to generate one.

Why need a seed to create a random number?

Quick answer: if you want to have control of the random sequence or let other people generate the same sequence of random numbers, take a seed.

We should know that a random number generated by the computer is based on some algorithm written by programmers. It means that it is not 100% random like the random process that you find in nature. In other words, we call it pseudo-random.

The random generator is somehow complicated, but for demo purposes, I assume that:

    \[aRan_{i} = seed + X_{i}.\]

So if you select the same seed, then you can have the same sequence.

seed in Python.

import random
random.seed(10)
print([random.randint(0, 9) for _ in range(5)])

if you run the code, you will always have the same sequence as mine:

[5, 4, 5, 2, 8]

seed in C#

using System;
public class Program
{
	public static void Main()
	{
		Console.WriteLine("Hello World");
		Random rdm = new Random(4);//seed = 4
		for(int i = 0; i < 5; i++){
			Console.Write(rdm.Next(9)+ " ");
		}
	}
}

And similarly, we have the same sequence :

7 8 5 3 0

Add comment

💬

A.I, Data and Software Engineering

PetaMinds focuses on developing the coolest topics in data science, A.I, and programming, and make them so digestible for everyone to learn and create amazing applications in a short time.

Categories