Skip to content
  • YouTube
  • FaceBook
  • Twitter
  • Instagram

Data Analytics Ireland

Data Analytics and Video Tutorials

  • Home
  • Contact
  • About Us
    • Latest
    • Write for us
    • Learn more information about our website
  • Useful Links
  • Glossary
  • All Categories
  • Faq
  • Livestream
  • Toggle search form
  • How to delete a key from a Python dictionary Python
  • TypeError: ‘list’ object is not an iterator Python
  • How to Compare Column Headers in CSV to a List in Python CSV
  • String Manipulation in Python python method
  • How to create a combobox in tkinter Python
  • TypeError: ‘float’ object is not callable Python
  • R tutorial – How to sort lists using rstudio R Programming
  • How to check if a file is empty Python

How to Generate Random Integers Between 0 and 9

Posted on November 17, 2021January 15, 2022 By admin

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?

  1. Computer simulation – look to generate random numbers as part of a simulation in a computer program.
  2. Statistcial sampling – If you are conducting some analysis on a set of data, you use the below to generate samples in a range.
  3. Introduce randomness – Helps with testing in different scenarios to ensure that does not allow any bias be introduced.
  4. Cryptography – It can be used as part of of security testing to make data secure in transmission or on a database.
  5. 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.

Python, random number generation Tags:Data Analytics, floating point, generate random number, randbelow, randit, random number, randrange

Post navigation

Previous Post: How to Automate Testing With Python | unittest
Next Post: How to use parameters in Python

Related Posts

  • What are Lambda functions in Python? Python
  • TypeError: ‘int’ object is not callable Python
  • How to change the headers on a CSV file CSV
  • How to Add Formulas to Excel using Python numpy
  • How to Automate Testing With Python | unittest automation
  • Tkinter python tutorial Python

Select your language!

  • हिंदी
  • Español
  • Português
  • Français
  • Italiano
  • Deutsch
  • How to group your data in Tableau data visualisation
  • Python Tutorial: How to validate data using tuples Python Tuples
  • How to Group By in a Select Statement SQL
  • How to check if a file is empty Python
  • how to select all records with SQL SQL
  • What is the difference between CHAR and VARCHAR2 in SQL? SQL
  • how do I merge two dictionaries in Python? Python
  • How do I fix TypeError: unhashable type: ‘list’ Error? Articles

Copyright © 2023 Data Analytics Ireland.

Powered by PressBook Premium theme

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT