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

Random numbers

时间: 2024-11-06 00:03:28

Random numbers的相关文章

Generating Gaussian Random Numbers(转)

Generating Gaussian Random Numbers http://www.taygeta.com/random/gaussian.html This note is about the topic of generating Gaussia  pseudo-random numbers given a source of uniform  pseudo-random numbers. This topic comes up more frequently than I woul

Javascript中,数学方法可以分成以下几类: constans(常数)、power functions(乘方函数)、trigonometic functions(三角函数)、rounding functions(舍入函数)、random numbers(随机数字)

在Javascript中,数学方法可以分成以下几类: constans(常数).power functions(乘方函数).trigonometic functions(三角函数).rounding functions(舍入函数).random numbers(随机数字) 常数和乘方函数 Math.E 自然对数的底(为常数) 2.718 Math.LN10 10的自然对数 2.302 Math.LN2 2的自然对数 0.693 Math.PI 圆周率 3.1415 Math.SQRT1_2 1/

[Python] Generating random numbers using numpy lib

import numpy as np def test_run(): data=np.random.random((3,4)) """ [[ 0.80150549 0.96756513 0.18914514 0.85937016] [ 0.23563908 0.75685996 0.46804508 0.91735016] [ 0.70541929 0.04969046 0.75052217 0.2801136 ]] """ data=np.ra

【DFS序+树状数组】BNUOJ 52733 Random Numbers

http://acm.bnu.edu.cn/v3/problem_show.php?pid=52733 [题意] 给定一棵树,这棵树每个点都有一个点权,标号从0开始,0是根结点 修改操作: SEED 1 13 把结点1的点权乘上13 查询操作: RAND 1 查询结点1为根的子树所有结点权的总乘积,以及这个总乘积有多少个因子 题目保证结果的质因子最大为13 [思路] 质因子最大为13,那么我们可以枚举质因子2 3 5 7 11 13,共6个 只要我们知道子树乘积这六个质因子的个数,我们就能算出子

K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)

题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子树上的所有节点的权值的乘积x,及x的因数个数. 2.SEED:将节点u的权值乘以x. 思路: 比赛时少看了因数不大于13这句话,然后本题难度增加数倍,肝了两个小时都没肝出来,对不起队友啊,今天的组队训练赛实力背锅…… 这题一眼线段树,由于是对一棵子树进行处理,因此我们采用常规套路,借助dfs序将子树

numpy的random模块

[说明] 翻译自官网的文档. 随机抽样 (numpy.random) 简单的随机数据 rand(d0, d1, ..., dn) 随机值 >>> np.random.rand(3,2) array([[ 0.14022471, 0.96360618], #random [ 0.37601032, 0.25528411], #random [ 0.49313049, 0.94909878]]) #random randn(d0, d1, ..., dn) 返回一个样本,具有标准正态分布.

How to generate a random number in R

Generate a random number between 5.0 and 7.5x1 <- runif(1, 5.0, 7.5) # 参数1表示产生一个随机数x2 <- runif(10, 5.0, 7.5)# 参数10表示产生10个随机数 Generate a random integer between 1 and 10x3 <- sample(1:10, 1)  # 参数1表示产生一个随机数x4 <- sample(1:10, 5, replace=T) # 参数5表

python的random模块

As an example of subclassing, the random module provides the WichmannHill class that implements an alternative generator in pure Python. The class provides a backward compatible way to reproduce results from earlier versions of Python, which used the

UVA Pseudo-Random Numbers

 Pseudo-Random Numbers  Computers normally cannot generate really random numbers, but frequently are used to generate sequences of pseudo-random numbers. These are generated by some algorithm, but appear for all practical purposes to be really random