April 28, 2018

Random value example in Python

The random values can be created using "random" module.


<Source code>
import random
import string

print random.random()
print random.uniform(10,20)
print random.randint(0,4)


dice = [1,2,3,4,5]
random.shuffle(dice)
print dice

p = range(0, 45)
print random.sample(p,7)

print(random.choice(string.ascii_lowercase))


<Result>
0.795117516183
10.1632118343
2
[1, 4, 3, 5, 2]
[36, 22, 17, 8, 28, 40, 30]
t

❑ random.random() : Return a random float type value ranging 0~1.
❑ random.uniform([start], [end]) : Return a random float type value ranging [start]~[end].
❑ random.randint([start], [end])Return a random int type value ranging [start]~[end].
❑ random.shuffle([list]) : Shuffle the [list].
❑ random.sample([list], [count]) : Return a sample list that selected random [count] values of [list]. 
❑ random.choice([sequence]) : Return one value from [sequence].