Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

python random

import random

# print a random integer number between 1 - 2 The stop number is not included
print(random.randrange(start=1, stop=3))

# print a random integer number between 1 - 2 The stop number is included
print(random.randint(1, 2))

# print a random float number between two numbers the end number is also included in the random selection
print(random.uniform(1, 2))

# generates random bits - print a random 8 bit integer
print(random.getrandbits(8))

# print a random choice from an iterable object
# prints 1, 2 or 3
print(random.choice([1, 2, 3]))

# prints a, b or c
print(random.choice('abc'))

# print random choices from an iterable object
# population is the iterable object. weights if for the possibility of outcome, k is for how many items to returns
# weights and k arguments are optional

# print a list of 10 heads or tails with an equal chance that it will be heads or tails
print(random.choices(population=["heads", "tails"], weights=[.5, .5], k=10))

# print a list of 10 heads or tails with a more likely chance of tails chance that it will be heads or tails
print(random.choices(population=["heads", "tails"], weights=[2, 10], k=10))

# stuff a list and print the list in shuffled ordered
mylist = [1, 2, 3]
random.shuffle(mylist)
print(mylist)

# return a random sample of an iterable - k is the number of the sample to return
# print two random characters from a string abc
print(random.sample(population="abc", k=2))

# print 3 random list items from a list
print(random.sample(population=[1, 2, 3, 4], k=3))

# print a random number float between two numbers the mode allows a higher possible toward either the low or high value
# the mode in this example will print a random float more closes to the high value of 50
print(random.triangular(low=10, high=50, mode=40))

Source by codefreelance.net #
 
PREVIOUS NEXT
Tagged: #python #random
ADD COMMENT
Topic
Name
6+9 =