## A Guide to Python Random Number Functions

Hello guys! Welcome back to another section of my tutorial on Python. In this tutorial guide, we are going to be discussing about the Python Random Number Functions. We are going to be starting with the Number choice() function.

# Python Number choice() Function

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

# Syntax

Following is the syntax for 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 by 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 below 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

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

# Syntax

Following is the syntax for 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 by using random static object.

# Parameter Details

• start - Start point of the range. This would be included in the range.
• stop - Stop point of the range. This would 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 below 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

The 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 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 by 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 below 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

Python number seed() function sets the integer starting value that is used in generating random numbera. 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 by using random static object.

## Parameter Details

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

## Return Value

This function does not return any value.

## Example

The following example below 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 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 by 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 below 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

The 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 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 by 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 below 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 my next tutorial, we are going to be discussing about the Trigonometric Functions in Python.

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.
Share: