# Module random. Functions for sequences

### Contents

Search other websites:

##### 1. Function random.choice(). Get random item in sequence

The random.choice() function is designed to get a random item from a non-empty sequence. The general form of function

`random.choice(sequence)`

here sequence – some nonempty sequence of values, which can be numbers, strings, etc.

If you specify an empty sequence, an IndexError exception will be thrown, which occurs when the sequence index is out of range.

Example. In the example, two sequences are formed. The first sequence is the days of the week. The second sequence is arbitrary numbers. In both cases, the choice() function will be called to select a random value.

```# Function random.choice()
# include the random module
import random

# 1. Form a strings sequence
days = ('Sun', 'Mon', 'Tue', 'Wed', 'Thi', 'Fri', 'Sat')

# get a random item from a sequence
rand_day = random.choice(days)
print(rand_day)

# 2. To generate the sequence of numbers
intNumbers = ( 2, 17, -11, -20, 33)

# get a random element from a sequence
randIntNumber = random.choice(intNumbers)
print(randIntNumber)```

Each time after starting the program for execution, a random element from the sequence will be randomly selected. One of the cases of obtaining the result of the program

```Wed
-20```

### ⇑

##### 2. Function random.choices(). Get a list of items from a population

This function has been introduced since Python 3.6. The random.choices (population, k) function returns a list of elements from the population set. The size of the resulting list is k.

According to Python documentation, the general form of the function is as follows

`random.choices(population, weights=None, *, cum_weights=None, k=1)`

here

• population – some population (set) of items. If population is empty, an IndexError exception is thrown;
• k – the number of items in the resulting list, which is obtained from the population set;
• weights – a sequence that sets relative weights when selecting items from a population set. Before making a selection, the relative weights of the weights are converted to the cumulative weights of cum_weights;
• cum_weights – alternate sequence to weights. If the cum_weights sequence is specified, then cumulative weights are set when selecting elements from the population set. For example, relative weights [1, 3, -2, 5] are equivalent to cumulative weights [1, 4, 2, 7].

Example.

```# Function random.choices()
# include module random
import random

# form a population
seq1 = [ 'а', 'b', 'с', 'd', 'е', 'f', 'g', 'h' ]

# form a new sequences from the seq1 population
seq2 = random.choices(seq1, [1, 1, 2, 1, 2, 3, 3, 3], k=6)
print('seq2 = ', seq2)

seq3 = random.choices(seq1, [5, 5, 5, 5, 5, 5, 5, 5], k=3)
print('seq3 = ', seq3)

seq4 = random.choices(seq1, [1, 1, 1, 1, 1, 1, 1, 1], k=3)
print('seq4 = ', seq4)

seq5 = random.choices(seq1, [1, 1, 1, 1, 1, 1, 1, 1], k=10)
print('seq5 = ', seq5)```

The result of the program

```seq2 = ['g', 'g', 'c', 'g', 'f', 'g']
seq3 = ['c', 'f', 'c']
seq4 = ['a', 'c', 'e']
seq5 = ['g', 'h', 'h', 'd', 'h', 'h', 'd', 'h', 'f', 'g']```

### ⇑

##### 3. Function random.shuffle(). Change the sequence

Function random.shuffle() is applied to sequences. The function arbitrarily changes the positions of the items of the sequence.

General form of function call

`random.shuffle(sequence[, random])`

here sequence – some sequence that may contain items of different types.

Example.

```# Function random.shuffle()
# include module random
import random

# 1. Form the sequence of integers
arr = [ 2, 3, 4, 5 ]
print(arr)

# shuffle the elements of sequence
random.shuffle(arr)
print(arr)

# 2. Form a sequence of real numbers
arr2 = [ 1.8, -2.8, 3.5, 4.77, -11.2 ]
print(arr2) # display the previous option

# shuffle the elements of sequence
random.shuffle(arr2)
print(arr2)

# 3. Form a sequence of items of different types
sequence = [ 'abc', 0.223, 25, True]
print(sequence)

# shuffle the elements of sequence
random.shuffle(sequence)
print(sequence)```

The result of the program

```[2, 3, 4, 5]
[2, 4, 5, 3]
[1.8, -2.8, 3.5, 4.77, -11.2]
[1.8, 4.77, -2.8, -11.2, 3.5]
['abc', 0.223, 25, True]
[0.223, True, 25, 'abc']```

### ⇑

##### 4. Function random.sample(). Get a list

The random.sample() function creates a new sequence of unique items of a given size. A sequence of unique items is selected from a certain set or population. The previous sequence does not change.

The general form of function

`random.sample(population, k)`

here

• population – some population that serves as input to form the resulting sequence;
• k – size of the resulting sequence.

Example.

```# Function random.sample()
# include module random
import random

# 1. Form the sequence of items
arr = [ 'a2', 3, '4', 5, False, 5, 5, 5 ]
print("arr = ", arr)

sample = random.sample(arr,3) # sample = [5, 'a2', '4']
print('random.sample(arr,3) = ', sample)

sample = random.sample(arr,6) # sample = [5, 5, 'a2', '4', 5, False]
print('random.sample(arr,6) = ', sample)

# 2. The sequence of numbers
numbers = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # 10 numbers
sample = random.sample(numbers,10) # all items

print('numbers = ', numbers)
print('random.sample(numbers,10) = ', sample)```

The result of the program

```arr = ['a2', 3, '4', 5, False, 5, 5, 5]
random.sample(arr,3) = [5, 3, 'a2']
random.sample(arr,6) = [5, '4', 'a2', 5, 3, 5]
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
random.sample(numbers,10) = [0, 6, 5, 3, 2, 7, 1, 8, 9, 4]```