• 随机数种子random.seed()理解


    总结:

    若采用random.random(),每次都按照一定的序列(默认的某一个参数)生成不同的随机数。

    若采用随机数种子random.seed(100),它将在所设置的种子100范围内调用random()模块生成随机数,如果再次启动random.seed(100),它则按照之前的序列从头开始生成随机数,两次生成的随机序列相同。

    若采用random.seed(),它则按照默认的一个序列生成随机数。

    程序演示:

    # -*- coding: utf-8 -*-
    """
    Created on Thu Nov 7 17:27:28 2019

    @author: Mr.King
    """

    import random
    random.seed(100)
    print("----------random.seed(100)-----------------")
    print("The first random number: ", random.random())
    print("The second random number: ", random.random())
    print("The third random number: ", random.random())

    random.seed(100)
    print("----------再次调用random.seed(100)----------")
    print("The fourth random number: ", random.random())
    print("The fifth random number: ", random.random())

    random.seed()
    print("----------random.seed()--------------------")
    print("The sixth random number: ", random.random())
    print("The seventh random number: ", random.random())

    运行结果:

    ----------random.seed(100)----------------------
    The first random number: 0.1456692551041303
    The second random number: 0.45492700451402135
    The third random number: 0.7707838056590222
    ----------再次调用random.seed(100)----------
    The fourth random number: 0.1456692551041303
    The fifth random number: 0.45492700451402135
    ----------random.seed()---------------------------
    The sixth random number: 0.20294571682496443
    The seventh random number: 0.05551047377535656

    ----------------------------------- 心之所向,素履所往;生如逆旅,一苇以航。 ------------------------------------------
  • 相关阅读:
    java实现网络监听
    程序员必须知道FTP命令
    Java进化的尽头
    Oracle逻辑备份与恢复(Data Pump)
    JQuery日期选择器插件date-input
    大型高并发高负载网站的系统架构剖析
    万言万当,不如一默为官之道
    angular.js高级程序设计书本开头配置环境出错,谁能给解答一下
    安装meteor运行基本demo发生错误。
    nodejs 通过 get获取数据修改redis数据
  • 原文地址:https://www.cnblogs.com/wzw0625/p/11813818.html
Copyright © 2020-2023  润新知