zoukankan      html  css  js  c++  java
  • 8.python内置模块之random模块简介

    Python中的random模块用于生成随机数。

    常用的7个函数:

    1.random.random():返回一个[0,1)之间的随机浮点值(双精度)

    2.random.uniform(a,b):返回[a,b]之间的一个随机浮点;a,b两个变量大小随意

    3.random.randrange([start],end,[step]):返回[start,end)的范围之间的一个随机整数,考虑步长

    4.random.randint(a,b):返回[a,b]之间的一个随机整数

    5.random.choice(seq):随机获取seq中的某个元素返回

    6.random.sample(seq,num):从seq对象中随机获取num个元素,以列表的形式返回

    7.random.shuffle(lt):将list对象lt中的元素打乱  【注意】只能接受list对象

     1 import random
     2 
     3 print(random.random())
     4 
     5 print(random.uniform(10,20))
     6 
     7 print(random.randrange(0,10,2))
     8 
     9 print(random.randint(1,3))
    10 
    11 print(random.choice('abcdefg'))
    12 
    13 print(random.sample(('a','b','c','d','e','f'),3))
    14 
    15 lt = [1,2,3,4,5,6,7]
    16 random.shuffle(lt)
    17 print(lt)
  • 相关阅读:
    1697 ⑨要写信
    1220 数字三角形
    4979 数塔
    bzoj1618[Usaco2008 Nov]Buying Hay 购买干草
    bzoj1066[SCOI2007]蜥蜴
    bzoj1008[HNOI2008]越狱
    cf437D The Child and Zoo
    cf437C The Child and Toy
    cf437B The Child and Set
    cf437A The Child and Homework
  • 原文地址:https://www.cnblogs.com/bonheur/p/12316397.html
Copyright © 2011-2022 走看看