All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Generate Random Numbers In Python Without Using Random

Last Updated : Mar 11, 2024

How To Generate Random Numbers In Python Without Using Random

In this tutorial we will show you the solution of how to generate random numbers in python without using random, the use of randomness is an important part of the configuration and evaluation of machine learning algorithms.

From the random initialization of weights in an artificial neural network, to the splitting of data into random training and test sets, to the random shuffling of a training dataset in stochastic gradient descent, generating random numbers and harnessing randomness is a required skill. So, let’s get started.

Step By Step Guide On How To Generate Random Numbers In Python Without Using Random :-

To generate random numbers without using the “random” module, we use time.

Time is a built-in module in Python and has several functions.

import time
def rand_num(minimum,maximum):
    now = str(time.perf_counter())
    rnd = float(now[::-1][:3:])/1000
    print(minimum + rnd*(maximum-minimum))
min = 1
max= 20
rand_num(min, max)
  1. In the first line there is an import statement that will import the time module into the program.
  2. After that, a function is defined, and the name of that function is “rand_num()”. This function will take two values as arguments: one is the lower limit from which you want to generate random numbers;
  3. The second one is the upper limit to which you want to generate random numbers. In this case, we have named these two arguments as minimum and maximum, respectively. The “rand_num()” function generates a random number between this minimum and maximum.
  4. In the next line, there is a variable named “now” that will store the value in the string data type returned from the “time.perf_counter()” function.
  5. The “time.perf_counter()” function always returns a floating time value in seconds. The “str()” function will convert the resultant time returned from the function into a string data type.
  6. In the next line there is a variable named “rnd”, which means random, that will store the result generated from the expression. The value will be converted into a float data type with the help of the “float()” function.
  7. In the next line there is a print statement that will display the value generated from the expression. The expression will first perform a subtraction of both the parameters, minimum and maximum.
  8. After that, the result of the subtraction will be multiplied by the value of the variable “rnd”. After that, the resultant value will be added to the value of the variable “minimum”.
  9. This is all for the “rand_num()” function. After that, outside the “rand_num()” function, there are two variables named “min” and “max” respectively.
  10. The value of the variable “min” is 1, but you can give any value you want. The value of the variable “max” is set to 20, but you can modify it however you want.
  11. In the last line, we will call the “rand_num()” function with the variables “min” and “max” passed in as the arguments.

Conclusion :-

So finally, in conclusion, we can say that with the help of this tutorial, you can now generate random numbers without using the random module in Python.

This is the simplest method you can use in your program.

I hope this tutorial on how to generate random numbers in python without using random helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪