Generator Random Numbers

Generator Random Numbers is a tool that generates Random numbers given maximum and minimum. For example, if we want to generate a number between 0 to 10, The Random Generator Will Give a random number between 0 to 10. Today we will write code in Python and C++ to develop the Random Numbers Generator.

Generator Random Numbers
Generator Random Numbers

Why Generating Random Numbers are Important

Why Random Numbers important
Why Random Numbers important

The setting and assessment of machine learning algorithms rely heavily on using randomness.

The ability to generate random numbers and harness randomness is necessary for many processes, such as the random initialization of weights in an artificial neural network, the random division of data into train and test sets, and the random shuffle of a training dataset in stochastic gradient descent.

Ways in Python

Python Random Numbers
Python Random Numbers

1. Python Built-in Function

random is a module in Python. ‘Import random‘ imports the module.

Example

import random

print(random.randint(3, 9))

The randint() method returns an integer number selected element from the specified range.

Syntax
random.randint(start, stop)

start Required. An integer specifying at which position to start.
stop Required. An integer specifying at which position to end.

Output: Changes everytime you run the Program.

You can check out the official documentation of the Python random module here: link

2. Using numpy Array

Numpy
Numpy

NumPy offers the random module to work with random numbers.

from numpy import random

x = random.randint(100)

print(x)

The Above program returns a random number between 0 to 100.

In NumPy we work with arrays, and you can use the two methods from the above examples to make random arrays.
The randint() method takes a size parameter where you can specify the shape of an array.

from numpy import random

x = random.randint(100, size=(3, 5))

print(x)

The Above program outputs a random array of size 3×5 with random values ranging from 0 to 100.

Ways in CPP

CPP Generator Random Numbers
CPP

1. Using rand() Function

The built-in C++ STL function rand() is defined in the header file cstdlib. rand() function produces random numbers. If every time this function is used, an algorithm that produces a succession of unrelated integers is used to generate the random number. For example when we use this function again and again we will get different outputs every time we run it.
In C++, the rand() function produces random integers between 0 and RAND MAX.

// C++ program to demonstrate
// the use of rand()
#include <cstdlib>
#include <iostream>
using namespace std;

int main()
{
	// This program will create same sequence of
	// random numbers on every program run
	for (int i = 0; i < 5; i++)
		cout << rand() << " ";

	return 0;
}

Output
1804289383 846930886 1681692777 1714636915 1957747793

2. Using stand() Function

Similarly stand() is also a built-in function of the C++ STL, which is a header file specified in cstdlib. The srand() function establishes the initial value for a stream of pseudo-random numbers. The rand() seed is set as if srand(1) had been called at program startup if srand() is not invoked. Different beginning point starts the generator if seed is set to any other value.

// C++ program to generate random numbers
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;

int main()
{
	// This program will create different sequence of
	// random numbers on every program run

	// Use current time as seed for random generator
	srand(time(0));

	for (int i = 0; i < 4; i++)
		cout << rand() << " ";

	return 0;
}

Output is different Everytime.

READ MORE

Python-related posts Visit HERE

C/C++ related posts Visit HERE

Data Structures related posts visit HERE

Databases related posts Visit HERE

Algorithms related posts visit HERE

Data Science related posts visit HERE

Share the Knowledge