zoukankan      html  css  js  c++  java
  • Random numbers

    Most computer programs do the same thing every time they execute, given the same inputs, so they are said to be deterministic. Deterministic is usually a good thing, since we expect the same calculation to yield the same result. For some applications, though, we want the computer to be unpredictable. Games are an obvious example, but there are more.

    Making a program truly nondeterministic turns out to be not so easy, but there are ways to make it at least seem nondeterministic. One of them is to use algorithms that generate pseudorandom numbers. Pseudorandom numbers are not truly random because they are generated by a deterministic computation, but just by looking at the numbers it is all but impossible to distinguish them from random. The random module provides functions that generate pseudorandom numbers.

    The function random returns a random float between 0.0 and 1.0. Each time you call random, you get the next number in a long series.

                           

    The function randint takes parameters low and high and returns an integer between low and high (including both). And to choose an element from a sequence at random, you can use choice:

     

    The random module also provides functions to generate random values from continuous distributions including Gaussian, exponential, gamma, and a few more.

     

    from Thinking in Python

  • 相关阅读:
    python -- 内存与垃圾回收源码分析
    机器学习:决策树
    leetcode -- 树操作
    树:基本树形
    树:遍历算法
    查找:字符串匹配算法 -- KMP
    Java 8 新特性
    Java学习书籍推荐
    Guava基本工具--Throwables:简化异常和错误的传播与检查
    Guava基本工具--排序: Guava强大的”流畅风格比较器”
  • 原文地址:https://www.cnblogs.com/ryansunyu/p/3912862.html
Copyright © 2011-2022 走看看