from random import choice code example

Example 1: random number python

# generate random integer values
from random import randint

value = randint(0, 10)
print(value)

Example 2: how to get a random element from an array in python

import random
names=['Mark', 'Sam', 'Henry']

#Set any array
random_array_item=random.choice(names)

#Declare a variable as a random choice
print(random_array_item)

#Print the random choice
#Or, if you want to arrange them in any order:
for j in range(names):
  print(random.choice(names))

Example 3: python random

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

Example 4: how to randomly choose from a list python

random.choice(name of list)

Example 5: random picker in python

import random
#dictionary
x_dict = {30:60, 20:40,10:20}
key = random.choice(list(x_dict))
print (key)#if you want it to print 30, 20, or 10
print (x_dict[key])#if you want it to print 60, 40, or 20
print (key,"-", x_dict[key])# if you want to print 30 - 60, 20-40,or 10-20

Example 6: random.choice()

import random
l=[1,2,3,4,5,6,7,8,9,0]
random.choice(l)
print(l);

Tags: