Python generate random number and string (Complete tutorial)

In this Python tutorial, let us discuss on Python generate random number, how to generate random number in Python. Also, we will discuss how to generate random strings in Python.

Let us discuss the below points:

  • Python random number between 0 and 1
  • Python random number integers in the range
  • Python random numbers between 1 and 10
  • Python random number seed
  • Python random number of choice
  • Python random integer number multiple of n
  • Python random number shuffle
  • Python random number between two floats
  • Python random number with n digits
  • Python random number without repeating
  • Python random 10 digit number
  • Python random string of length
  • Python random string of from letters
  • Python random string of letters and numbers
  • Python random string with special character
  • Python random string from alphabets

Python generate random number

Let us see various ways to generate random number in Python. We will discuss different ways to generate random numbers in Python.

In this example, I have imported random module,random() returns the random floating value.

Example:

import random
random_number = random.random()
print(random_number)
Python generate random number
Python generate random number

This way, we can create a single random number in Python.

You may like Draw colored filled shapes using Python Turtle and How to Create a Snake game in Python using Turtle

Python random number between 0 and 1

Now we can see how to get a random number between 0 to 1 in python

To get any random number we have to first import random module. random.uniform()is used to get a random number, The uniform() returns random floating-point numbers between the two given numbers.

Example:

import random
number = random.uniform(0,1)
print(number)

Below screenshot shows the output:

r1
Python random number between 0 and 1

Python random number integers in the range

Here we can see how to get a random number integers in the range in python,

In this example we can see how to get a random number when the range is given, Here I used the randint() method which returns an integer number from the given range.in this example, the range is from 0 to 10.

Example:

import random  
random_number = random.randint(0, 9) 
print(random_number) 

Below screenshot shows the output:

r2
Python random number integers in the range

Python random numbers between 1 and 10

Here we can see how to get a random numbers between 1 and 10 in python

In this example,I have used randint() which returns a random number when range is specified.

Example:

import random
print(random.randint(1,10))

Below screenshot shows the output:

r3
Python random numbers between 1 and 10

Python random number seed

We can use the Python Seed function to initialize a random number in Python. By default, it will create a random number by using the current time of the system.

To get customize the start number of the random number generator, we can use seed() function

Here we can see how to generate a random number using seed() function in python.

Example:

import random

random.seed(15)
print(random.random())

Below screenshot shows the output:

r4
Python random number seed

Python random number of choice

In this, we can see how to get a random number from the specified sequence of list or string in python

In this example, random.choice() is used, which returns a random number from the specified Python list.

Example:

import random

numberList = [1,22,58,14,25,24,]
print( random.choice(numberList))

Below screenshot shows the output:

r5
Python random number of choice

Python random integer number multiple of n

Now we can see how to get a random integer number multiple of n in Python.

Here we can generate a random number which is multiple of 4 from the range 4 to 200.and used print(num) to print the random number.

Example:

import random

num = random.randrange(4, 200, 4)
print(num)

Below screenshot shows the output:

r6
Python random integer number multiple of n

Python random number shuffle

In this we can see how to get a random number using shuffle() in python.

In this example, some numbers are assigned to a list number and random.shuffle() is used to get random shuffle number from the list.

The shuffle() method takes a sequence of numbers in the list and reorganizes the order of numbers in the list randomly.

Example:

import random

number = ["1","5","2","3"]
random.shuffle(number)

print(number)

Below screenshot shows the output:

r8
Python random number shuffle

Python random number between two floats

Now we can see how to get a random numbers between two floats in python

In this example, we have to import a random module, and random.uniform() function is used to get a random number.

The uniform() returns random floating-point numbers between the two given floating numbers.

Example:

import random
x = random.uniform(0.5, 1.5)
print(x)

Below screenshot shows the output:

r9
python random number between two floats

Python random number with n digits

Now we can see how to get a random number with n digits in python.

To get a random number with n digits random.radint() is used which returns the random number of length of two given digits. The length of the random number will be equal to the length of two given numbers.

Example:

import random
random_number = random.randint(1000, 2000)
print(random_number)

Below screenshot shows the output:

r10
python random number with n digits

Python random number without repeating

In this we can see how to get a list of random numbers without repeating in python.

In this example, I have used Python random.sample() function which generates the unique random number within the given range of numbers.

Example:

import random
list = random.sample(range(0, 10), 10)
print(list)

Below screenshot shows the output:

r11
Python random number without repeating

This is how we can generate a random number in Python using random.sample().

Python random 10 digit number

Here we can see how to generate a random 10 digit number in Python. To get a random number of specified lengths we should set maximum and minimum values.

In this example, I have imported a module random and to get a random number we should generate N and assign minimum – minimum = pow(10, N-1) and maximum – maximum = pow(10, N) – 1 and then return a random number of length as given in the argument.

Example:

import random

def randomnumber(N):
	minimum = pow(10, N-1)
	maximum = pow(10, N) - 1
	return random.randint(minimum, maximum)

print(randomnumber(10))

Below screenshot shows the output:

Python random 10 digit number
Python random 10 digit number

This is how we can generate a random 10 digit number in Python very easily.

Python random number using sample()

Now we can see how to generate random number using sample().

In this example, I have imported random module and sample module from random, I have taken a list as numbers and print () contains a sample(), a list, and the size of the list. The sample() returns the random list from the specified list.

Example:

from random import sample 
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9]  
print(sample(numbers,5)) 

Below screenshot shows the output:

n3
Python random number using sample()

Python generate random string

Now, let us see how to generate random string in Python.

In this example, I have imported two modules they are random and string and to get a random string

Call string. ascii is initialized to get a constant string, string.ascii_uppercase gives only uppercase letters. and choice() returns randomly selected elements from any sequence.

Example:

import random
import string
letters = string.ascii_uppercase
print ( ''.join(random.choice(letters)))

Below screenshot shows the output:

r12
Python Random String

Python random string of length

Here we can see how a random string of specified length in python

In this example, I have used length_of_string = 4, It is the length of string function returns the number of character in the input string. and also the range() function is used to get the range of length of the string,

And choice() returns randomly selected elements from any sequence.

Example:

import random
import string
length_of_string = 4
letters = string.ascii_uppercase
random_string = ""
for number in range(length_of_string):
  random_string += random.choice(letters)
print(random_string)

Below screenshot shows the output:

r13
Python Random String

Python random string of list

Here we can see how to get a random string from a given list in python

In this example, a list is given and a random string is generated from that string itself. as above showed here also use the same random. choice() which returns randomly selected elements from any sequence.

Example:

import random
import string
print(random.choice(['mango', 'orange', 'apple']))

Below screenshot shows the output:

r14
python random string of choice

Python random string of letters and numbers

In this we can see how to get random string of both letters and numbers in python

In this example string join() is used which joins all the items from the string.

”.join(random.choices(string.ascii_uppercase + string.digits, k = N) this method joins random alphabets and digits.

Example:

import string 
import random 
N = 5
res = ''.join(random.choices(string.ascii_uppercase +  string.digits, k = N)) 
print("The random string : " + str(res))

Below screenshot shows the output:

r15
Python random string of letters and numbers

Python random string with special character

Here we can see how to get a random string with special chracter in python

This example is similar to a random string of letters and numbers string punctuation is also added to it to get the special character in the string and string join() is used which joins all the items from the string.

import random
import string
def randomStringwithDigitsAndSymbols(stringLength=4):
  special_characters = string.ascii_letters + string.digits + string.punctuation
   return ''.join(random.choice(special_characters) 
for i in range(stringLength))
print (randomStringwithDigitsAndSymbols())
print (randomStringwithDigitsAndSymbols())
print (randomStringwithDigitsAndSymbols())

Below screenshot shows the output:

r16
Python random string with special character

Python random string from alphabets

Here we can see how to get random string only from alphabets in python,

In this example length of the string is used, and also string join() is used which joins all the items from the string. range () which returns the length range of the string.

Example:

import random
import string
string_length = 8
upper_case_alphabet = string.ascii_letters
lower_case_alphabet = string.ascii_letters
random_upper_letter = random.choice(lower_case_alphabet)
random_lower_letter = random.choice(upper_case_alphabet)

print(''.join(random.choice(random_upper_letter+random_lower_letter) for i in range(string_length)))

Below screenshot shows the output:

r17
Python random string from alphabets

Here, we saw different ways to generate random strings in Python.

You may like the following Python tutorials:

In this tutorial, we learn, how to generate random numbers in Python and how to generate random string in Python.

  • Python generate random number
  • Python random number between 0 and 1
  • Python random number integers in the range
  • Python random numbers between 1 and 10
  • Python random number seed
  • Python random number of choice
  • Python random integer number multiple of n
  • Python random number shuffle
  • Python random number between two floats
  • Python random number with n digits
  • Python random number without repeating
  • Python generate random string
  • Python random string of length
  • Python random string of list
  • Python random string of letters and numbers
  • Python random string with special character
  • Python random string from alphabets