We now have a youtube channel. Subscribe!

# A Guide to Python Random Number Functions

Hello dear readers! Welcome back to another edition of our tutorial on Python. In this tutorial guide, we are going to be discussing about the Random Number Functions in Python. We will be starting with the Number choice() function.

# Python Number choice() Function

Python number choice() function returns a random item from a list, tuple, or string.

# Syntax

Following below is the syntax for the choice() function -

`choice( seq )`

Note - This function is not directly accessible, so we need to import random module and then we need to call this function using random static object.

# Parameter Details

• seq - This could be a list, tuple, or string.

# Return Value

This function returns a random item.

# Example

The following example shows the usage of choice() function -

```#!/usr/bin/python
import random

print "choice([1, 2, 3, 5, 9]) : ", random.choice([1, 2, 3, 5, 9])
print "choice('A String') : ", random.choice('A String')```

# Output

Below is the output of the above example -

```choice([1, 2, 3, 5, 9]) :  2
choice('A String') :  n```

# Python Number randrange() Function

The number randrange() function returns a random selected element from the range(start, stop, step).

# Syntax

Following below is the syntax for the randrange() function -

`randrange( [start, ] stop [, step] )`

Note - This function is not directly accessible, so we need to import random module and then we need to call this function using random static object.

# Parameter Details

• start - The starting point of range. This would be added in the range.
• stop - The stopping point of range. This will be excluded from the range.
• step - Steps to be added in a number to decide a random number.

# Return Value

It returns a random item from the given range.

# Example

The following example shows the usage of randrange() function -

```#!/usr/bin/python
import random

# Select an even number in 100 <= number < 1000
print "randrange(100, 1000, 2) : ", random.randrange(100, 1000, 2)

# Select another number in 100 <= number < 1000
print "randrange(100, 1000, 3) : ", random.randrange(100, 1000, 3)```

# Output

Below is the output of the above example -

```randrange(100, 1000, 2) :  976
randrange(100, 1000, 3) :  520```

## Python Number random() Function

Python number random() function returns a random float r, such that 0 is less than or equal to r and r is less than 1.

## Syntax

Following below is the syntax for random() function -

`random( )`

Note - This function is not directly accessible, so we need to import random module and then we need to call this function using random static object.

• NA.

## Return Value

This function returns a random float r, such that 0 is less than or equal to r and r is less than 1.

## Example

The following example shows the usage of random() function -

```#!/usr/bin/python
import random

# First random number
print "random() : ", random.random()

# Second random number
print "random() : ", random.random()```

## Output

Below is the output of the above example -

```random() :  0.281954791393
random() :  0.309090465205```

## Python Number seed() Function

The number seed() function sets the integer starting value used in generating random numbers. Call this function before calling any other random module function.

## Syntax

Following is the syntax for seed() function -

`seed( [x] )`

Note - This function is not directly accessible, so we need to import random module and then we need to call this function using random static object.

## Parameter Details

• x - It is the seed for the next random number. If omitted, it takes the system much time to generate the next random number.

## Return Value

This function does not return any value.

## Example

The following example shows the usage of seed() function -

```#!/usr/bin/python
import random

random.seed( 10 )
print "Random number with seed 10 : ", random.random()

# It will generate same random number
random.seed( 10 )
print "Random number with seed 10 : ", random.random()

# It will generate same random number
random.seed( 10 )
print "Random number with seed 10 : ", random.random()```

## Output

Below is the output of the above example -

```Random number with seed 10 :  0.57140259469
Random number with seed 10 :  0.57140259469
Random number with seed 10 :  0.57140259469```

### Python Number shuffle() Function

Python number shuffle() function randomizes the items of a list in place.

### Syntax

Following below is the syntax for shuffle() function -

`shuffle( 1st )`

Note - This function is not directly accessible, so we need to import random module and then we need to call this function using random static object.

### Parameter Details

• 1st - This could be a list or a tuple.

### Return Value

This function does not return any value.

### Example

The following example shows the usage of shuffle() function -

```#!/usr/bin/python
import random

list = [20, 16, 10, 5];
random.shuffle(list)
print "Reshuffled list : ",  list

random.shuffle(list)
print "Reshuffled list : ",  list```

### Output

Below is the output of the above example -

```Reshuffled list :  [16, 5, 10, 20]
Reshuffled list :  [16, 5, 20, 10]```

RECOMMENDED POST: JavaScript Variables

### Python Number uniform() Function

Python number uniform() function returns a random float r, such that x is less than or equal to r and r is less than y.

### Syntax

Following below is the syntax for uniform() function -

`uniform( x,  y )`

Note - This function is not directly accessible, so we need to import random module and then we need to call this function using random static object.

### Parameter Details

• x - Sets the lower limit of the random float.
• y - Sets the upper limit of the random float.

### Return Value

This function return a floating point number.

### Example

The following example shows the usage of uniform() function -

```#!/usr/bin/python
import random

print "Random Float uniform(5, 10) : ",  random.uniform(5, 10)
print "Random Float uniform(7, 14) : ",  random.uniform(7, 14)```

### Output

Below is the output of the above example -

```Random Float uniform(5, 10) :  5.52615217015
Random Float uniform(7, 14) :  12.5326369199```

Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial, we are going to be discussing about the Python Trigonometric Functions.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.