Python - Numeric Random Number

Introduction

The standard library random module provides tools of:

  • as picking a random floating-point number between 0 and 1, and
  • selecting a random integer between two numbers:

Demo

import random 
print( random.random() )
print( random.random() )              # Random floats, integers, choices, shuffles 
print( random.randint(1, 10) )
print( random.randint(1, 10) )

Result

This module can also choose an item at random from a sequence, and shuffle a list of items randomly:

Demo

import random 
print( random.choice(['Json', 'Database', 'Web']) )
print( random.choice(['Json', 'Database', 'Web']) )

suits = ['hearts', 'clubs', 'diamonds', 'spades'] 
print( random.shuffle(suits) )
print( random.shuffle(suits) )
print( suits )# from  www  .  j  ava 2s . c o  m

Result