random.sample python code example

Example 1: python random

#import random 
import random

names = ['Harry', 'John', 'Smith', 'Larry']

#print random name from names
print(random.choice(names))

#print random integer in a range of numbers
print(random.randint(1, 100)

Example 2: python random

# imports random
import random
# randint generates a random integar between the first parameter and the second
print(random.randint(1, 100))
# random generates a random real number in the interval [0, 1)
print(random.random())

Example 3: choice random python

import random

#sampling with replacement
list = [20, 30, 40, 50 ,60, 70, 80]
sampling = random.choices(list, k=4)
print("Randomly selected multiple choices using random.choices() ", sampling)

Example 4: python choose sample from list with replacement

import random

random.choices(list, k = 4)

Example 5: random range python

from random import randrange
print(randrange(10))

Example 6: random.sample python

# imports random
import random
# randint generates a random integar between the first parameter and the second
print(random.randint(1, 100))

Tags:

Misc Example