- How to Get a Random Number in Python
- How to Generate a Random Float Between 0 and 1
- How to Generate a Random Integer Between 0 and 1
- How to Pick a Random Number from a List of Numbers
- How to Generate a Random Number Between 1 and 100
- How to Get a Normally Distributed Random Number in Python
- How to Simulate Coin Toss in Python—Heads or Tails
- Conclusion
- python random number between 1 and 100
How to Get a Random Number in Python
To get a random number in Python, use the randint() function from the random module.
For example, let’s generate a random integer between 1 and 100:
import random random_number = random.randint(1,100) print(random_number)
To generate a random float, use the uniform function from the random module:
import random random_float = random.uniform(0,100) print(random_float)
These are the two basic use cases you are going to need.
Let’s go through some of the most common questions related to generating random numbers in Python.
How to Generate a Random Float Between 0 and 1
To generate a random float between 0 and 1 , use the uniform() function from the random library.
import random random.uniform(0, 1)
How to Generate a Random Integer Between 0 and 1
To generate a random number that is either 0 or 1, use the randint() function from the random library.
import random random.randint(0, 1)
How to Pick a Random Number from a List of Numbers
To randomly choose a number from a list of numbers, use the choice() function from the random module.
import random numbers = [1, 2, 3, 4, 5] random_selection = random.choice(numbers) print(random_selection)
How to Generate a Random Number Between 1 and 100
To generate a random number between 1 and 100, use the uniform() function from the random library like this:
import random random_number = random.randint(1, 100) print(random_number)
How to Get a Normally Distributed Random Number in Python
To generate a random number from the Gaussian distribution, use the random library’s gauss() function.
For example, let’s generate a random number from a normal distribution with the mean of 5 and standard deviation of 10 :
import random # The mean of the distribution mu = 10 # The standard deviation of the distribution sigma = 5 print(random.gauss(mu, sigma))
How to Simulate Coin Toss in Python—Heads or Tails
To simulate a coin toss in Python, you need to randomly select between heads and tails.
To do this, create a list that has both heads and tails in it. Then choose one of them randomly using the random.choice() function.
Here is how it looks in code:
import random def coinToss(): return random.choice(['Tails', 'Heads']) print(coinToss())
Conclusion
To generate random numbers in Python, use the random module.
Thanks for reading. I hope you enjoy it.
python random number between 1 and 100
Using the random module, we can generate pseudo-random numbers. The function random() generates a random number between zero and one [0, 0.1 .. 1]. Numbers generated with this module are not truly random but they are enough random for most purposes.
Random number between 0 and 1.
We can generate a (pseudo) random floating point number with this small code:
from random import *
print(random()) # Generate a pseudo-random number between 0 and 1.
Generate a random number between 1 and 100
To generate a whole number (integer) between one and one hundred use:
from random import *
print(randint(1, 100)) # Pick a random number between 1 and 100.
This will printa random integer. If you want to store it in a variable you can use:
from random import *
x = randint(1, 100) # Pick a random number between 1 and 100.
print(x)
Random number between 1 and 10
To generate a random floating point number between 1 and 10 you can use the uniform() function
from random import *
print(uniform(1, 10))
Picking a random item from a list
Fun with lists
We can shuffle a list with this code:
from random import *
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
shuffle(items)
print(items)
To pick a random number from a list:
from random import *
items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
x = sample(items, 1) # Pick a random item from the list
print(x[0])
y = sample(items, 4) # Pick 4 random items from the list
print(y)
We can do the same thing with a list of strings:
from random import *
items = [‘Alissa’,‘Alice’,‘Marco’,‘Melissa’,‘Sandra’,‘Steve’]
x = sample(items, 1) # Pick a random item from the list
print(x[0])
y = sample(items, 4) # Pick 4 random items from the list
print(y)
If you are new to Python programming, I highly recommend this book.
the function «sample» does not work on my laptop with windows9
from random import *
items = ['Alissa','Alice','Marco','Melissa','Sandra','Steve']
x = sample(items, 1) # Pick a random item from the list
print x[0]
Which Python version are you using? Try replacing by print(x[0])
Im getting error in randint( )
>>> from random import *
>>> print randint(1,100)
SyntaxError: invalid syntax
I'm using Python 3.4.3
Hi Harsi, in Python 3 you need bracket around a print statement. Try this:
>>> from random import *
>>> print(randint(1,100))
Hmm could you guys tell me what does it: from random import * mean ?
Thanks 😀
Hi, this imports all the functions from a python file called random (actually random.py.
This file is one of the standard python modules. You can see the source code of random.py here: https://hg.python.org/cpython/file/2.7/Lib/random.py.
You can create your own module that way:
Create a file called test.py
#!/usr/bin/env python
def add(a,b):
return a+b
Then create a file called app.py:
from test import *
print(‘hello’)
print(add(5,2))
The program app.py now uses the code in test.py. The same principle applies when you use «from random import *»
items = [‘Alissa’,’Alice’,’Marco’,’Melissa’,’Sandra’,’Steve’]x = sample(items, 1) # Pick a random item from the list
print x[0]why we put [0] here ?
and what will happen if we change (items, 1) to (items, 2 or 3 or 4 )
In the presence of print x[0]This point needs to explain more.
The sample function can return a list of numbers. sample(items,2) would return a list of two random numbers. x[0] is simply the first random number in the list
Is the statement from random import * a seeding process?
Does this mean that y = sample(items,4) will return the same 4 items each time it is used?
Hi Steve, the statement y = sample(items,4) will return new items every call. If you want you can call the random class seed() function, which uses the system time to initialize.
Just a quick question about sample() — when used to fill values in an equation it returns:
TypeError: can’t multiply sequence by non-int of type ‘float’
I’m curious why this isn’t allowed, and if there is an alternative that I haven’t been able to find for extracting multiple random values from a series of value. For now, all I’ve come up with is using the choice() alternative with a loop for however large my sample pool needs to be, but I have a feeling there’s a better way to do it. Admittedly, I haven’t read through all of the documentation for random thoroughly, but I haven’t found a better solution. I figured that someone with a bit of knowledge would be my best bet. Any suggestions? As an aside, I should note I’m using 2.7 to learn with. I know they changed some stuff with Python 3.x, so I’m not sure if this is even an issue with newer revisions.
Are you multiplying with a sequence, perhaps something like this?:
seq = [1,2,3,4,5]y = seq * randomElement
To do so, we have to iterate over every element:
from random import *
items = [1, 2.13, 3.24, 4.25, 5.46, 6.57, 7.68, 8.79, 9.810, 10.911]seq = [1,2,3,4,5]# Get a list of 1 random numbers, convert to one random number.
randomElement = sample(items, 1)
randomElement = randomElement[0]# Multiple every element of the list
for element in seq:
y = element * randomElement
print(«y pcomment»>
I think you answered my question pretty well. My mistake was in not addressing the derived sample with an index value, so I was essentially attempting mathematics against a list rather than single values from within the list. It somehow slipped past me that sample returned a list. I guess it’s a beginner mistake and lack of attention on my part.
Quick questions, though:
Is the behavior of a loop iteration of choice() essentially the same as the returned sample() list? For clarification, a simple example would be a loop that iterates 4 times using choice() and storing the returned values in a list, versus a sample() of 4 the same values. Would that essentially return the same chance of random values?
Also, supposing they essentially return the similar randomness in their values, is there a performance difference over larger iterations? Common sense tells me that since the looping choice() jumps right to the chase, but a sample() first stores the values in a new list and that list then needs to be iterated over, looping choice() would be the cleaner and more efficient alternative. Am I mistaken?
Thanks for the quick response, I appreciate your answer and it definitely helped me understand where I originally went wrong.