Random number generation method

  • random.random()
    generates a 0-1 random floating point number

    import random
    random.random()
    

    Insert picture description here

  • random.uniform(a,b)
    generates a random floating point number in the specified range.

    random.uniform(1, 10)
    

    Insert picture description here

  • random.randint(a,b)
    generates an integer in the specified range

    random.randint(3, 7)
    

    Insert picture description here

  • random.randrange(start, stop, step)
    gets a random number from the set in the specified range, which increases by the specified base.

    random.randrange(2, 10, 3)
    

    Insert picture description here

  • random.choice(sequence)
    selects a value from the ordered type represented by sequence (not a specific type, but a series of types, list, tuple, string, etc.).

    random.choice('random function study!')
    random.choice(['random', 'function', 'study'])
    

    Insert picture description here

  • random.shuffle(x) is
    used to shuffle the elements in a list

    x = ['random', 'function', 'study']
    random.shuffle(x)
    

    Insert picture description here

  • random.sample(sequence, k)
    randomly obtains a fragment of the specified length from the specified sequence, and the sample function does not modify the original sequence.

    ls = [1, 3, 5, 7]
    random.sample(ls, 2)
    ls
    

    Insert picture description here

  • random.seed()
    The seed for random number generation. When the seed has no parameters, the random number generated each time is different, and when the seed has the same parameters, the random number generated each time is the same. Choose different parameters The random number generated is also different.

    #随机数不一样
    random.seed()
    random.random()
    random.seed()
    random.random()
    #随机数一样
    random.seed(1)
    random.random()
    random.seed(1)
    random.random()
    random.seed(2)
    ranodm.random()
    

    Insert picture description here

Guess you like

Origin blog.csdn.net/studyeboy/article/details/110856635
Recommended