Estimated reading time: 3 minutes
You may have a number of machine learning projects where you need a way to generate random integers to test your model assumptions.
There are plenty of ways to achieve this, here we will take you through a number of different steps.
To demonstrate this we take you through a number of different scenarios, but before we do that:
What are the uses of using one or more of the below?
- Computer simulation – look to generate random numbers as part of a simulation in a computer program.
- Statistcial sampling – If you are conducting some analysis on a set of data, you use the below to generate samples in a range.
- Introduce randomness – Helps with testing in different scenarios to ensure that does not allow any bias be introduced.
- Cryptography – It can be used as part of of security testing to make data secure in transmission or on a database.
- Stress testing – Pick extreme values to pass to a program and see how it reacts and if it returns an error.
You can use approach of the randrange function
In this function, this creates a randomly selected value that you need, in a range you define:
from random import randrange
print(randrange(1,10,1))
Result = prints a random number starting at 1, ending at 10, in increments of 1
Use the randint function?
import random
print(random.randint(0,9))
Result = This produces a random number integer value between 0 and 9
What about the concept of randbelow function?
from secrets import randbelow
print(randbelow(9))
Result = Returns a random number between 0 and 9
As a result you could try the numpy random.randit functionality as it is also popular
import numpy as np print(np.random.randint(10, size=(1, 10))) Result = prints ten integer values, anything between 1 and 10 Sample Output: [[6 8 3 9 4 1 5 3 2 7]]
Using the range function in a for loop is an option
n={} # Creates a dictionary of key value pairs
for i in range(10):
n[i]=i
print(n)
Result = Iterates and creates a dictionary as it moves through each step.
Sampele Output:
{0: 0}
{0: 0, 1: 1}
{0: 0, 1: 1, 2: 2}
{0: 0, 1: 1, 2: 2, 3: 3}
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4}
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5}
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6}
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7}
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8}
{0: 0, 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9}
Use random.randrange in a for loop
from random import randrange
for i in range(10):
print(random.randrange(0, 10))
Result = Loops ten times to create ten random numbers
Sample Output:
9
1
8
1
7
7
6
3
9
7
In this instance, while the focus of this post is on integers, we have two examples using floating points.
You can generate random floating points using random.uniform
import random
print(random.uniform(0.0, 9.0))
Result = Returns a random floating point number.
Sample Output:
4.436111014119316
An alternative is to generate random floating point values in a range
import random for i in range(10): print(random.random()) Result = Returns ten random floating point numbers. Sample Output: 8.80776539498274 0.8975905667079721 0.8467133530607382 0.757433819303854 0.88431126761758 0.9077189321042094 0.4826489161220854 0.7689417823093723 0.505382717614604 0.3102908090040003 0.01832993383665016
So in summary there are various different ways to create random numbers either a single one or a group of random numbers.
As a result of this, it is a very handy way to assist a programmer test a machine learning project.
On the other hand, if you need to try and break your program with values that are at the extreme of a range it can be accommodated this way as well.