How to Generate a Sequence of Numbers

Rate this post

Problem Formulation and Solution Overview

In this article, you’ll learn how to create a sequence of numbers in Python.

To make it more fun, we have the following running scenario:

Lux Lottery has decided to create a new Quick-Pick game called Lux-150. This game is based on seven (7) random numbers between 1 and 150 for a monthly payout of $150,000. The purchaser will not select these numbers individually but be automatically generated.

Your task as a Python Coder is to write a script that, when run, generates a new Lux-150 ticket sorted in ascending order and output to the terminal window.

💬 Question: How would we write the Python code to generate a list of numbers and accomplish this task?

We can accomplish this task by one of the following options:

  • Method 1: Use List Comprehension with random.randrange().
  • Method 2: Use For Loop with random.randint().
  • Method 3: Use sample().
  • Method 4: Use NumPy with random.randint.
  • Method 5: Use random.SystemRandom().

Preparation

Before any data manipulation can occur, two (2) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The NumPy library supports multi-dimensional arrays and matrices in addition to a collection of mathematical functions.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install numpy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.


Feel free to view the PyCharm installation guide for the required libraries.


Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd 
import numpy as np 
import random

Method 1: Use List Comprehension

Using List Comprehension is an excellent, efficient way to generate random numbers.

lotto_nums = [random.randrange(1, 151) for num in range(7)]
lotto_nums = sorted(lotto_nums)
print(lotto_nums)

List Comprehension generates seven (7) random Lottery numbers between 1 (start) and 151 (stop-1). The result saves to lotto_nums.

All in one line!

The sorted() function is called and passed as a parameter to present the new Lux-150 Lottery numbers in ascending order. Finally, the new Lux-150 ticket is output to the terminal window.

Output

[20, 25, 44, 51, 88, 117, 126]

Method 2: Use For Loop

Another way to perform this task is with a For Loop. Not as efficient as List Comprehension, but it still does the trick!

lotto_nums = []
for i in range(1, 8):
    lotto_nums.append(random.randint(1, 151))
lotto_nums.sort()
print(lotto_nums)

In this code, we declare an empty List, lotto_nums.

A For Loop is instantiated with the parameters: 1 (start) and 8 (stop-1).

Inside this loop, we use random.randint() and pass the parameters: 1 (start) and 151 (stop-1) to generate the random Lottery numbers. These numbers append and save to lotto_nums.

The sort() function is called to present the new Lux-150 Lottery numbers in ascending order. Finally, the new Lux-150 ticket is output to the terminal window.

Output

[11, 12, 19, 20, 59, 85, 107]

Method 3: Use random.sample()

An alternative approach is to use the random.sample() function, which is called from the random library.

lotto_nums = random.sample(range(1, 151), 7)
lotto_nums.sort()
print(lotto_nums)

Using the random.sample() function, we pass the parameters: 1 (start), 151 (stop-1), and 7 (size-inclusive). This generates seven (7) random Lottery numbers and saves to lotto_nums.

The sort() function is called to present the new Lux-150 Lottery numbers in ascending order.

Finally, the new Lux-150 ticket is output to the terminal window.

Output

[18, 23, 73, 82, 89, 96, 123]

Method 4: Use NumPy random.randint()

In this code, we use the NumPy library, np.random.randint() function.

lotto_nums = list(np.random.randint(low=1,high=151,size=7))
lotto_nums.sort()
print(lotto_nums)

In this code, we pass the np.random.randint() function three (3) parameters: low=1 (start), high=151 (stop-1) and size=7 (inclusive). This generates seven (7) random Lottery numbers and saves to lotto_nums.

The sort() function is called to present the new Lux-150 Lottery numbers in ascending order. Finally, the new Lux-150 ticket is output to the terminal window.

Output

[47, 62, 80, 80, 103, 112, 135]

Method 5: Use random.SystemRandom()

Use random.SystemRandom() if you would like to generate seven (7) Cryptographically Secure random Lottery numbers.

lotto_nums = []
rand_nums = random.SystemRandom()
for num in range(0,7):
	num = rand_nums.randint(1,151)
	lotto_nums.append(num)
lotto_nums.sort()
print(lotto_nums)

This code defines an empty list, lotto_nums, and seeds rand_nums.

A For Loop is instantiated and passed the parameters: 0 (start) and 7 (stop-1).

This loop generates seven (7) random Lottery numbers using rand_nums.randint() and passing the parameters: 1 (start) and 15 (stop). These numbers append and save to lotto_nums.

The sort() function is called to present the new Lux-150 Lottery numbers in ascending order. Finally, the new Lux-150 ticket is output to the terminal window.

Output

[19, 27, 46, 65, 73, 112, 126]

Summary

These five (5) methods of generating random numbers should give you enough information to select the best one for your coding requirements.

Good Luck & Happy Coding!