- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我是随机算法的新手,通过阅读书籍自学。我正在阅读 Mark Allen Wessis 的《数据结构和算法分析》一书.
Suppose we only need to flip a coin; thus, we must generate a 0 or 1 randomly. One way to do this is to examine the system clock. The clock might record time as an integer that counts the number of seconds since January 1, 1970 (atleast on Unix System). We could then use the lowest bit. The problem is that this does not work well if a sequence of random numbers is needed. One second is a long time, and the clock might not change at all while the program is running. Even if the time were recorded in units of microseconds, if the program were running by itself the sequence of numbers that would be generated would be far from random, since the time between calls to the generator would be essentially identical on every program invocation. We see, then, that what is really needed is a sequence of random numbers. These numbers should appear independent. If a coin is flipped and heads appears, the next coin flip should still be equally likely to come up heads or tails.
以下是关于上述文本片段的问题。
在上面的文本片段“我们可以使用最低位的秒数”中,作者提到这不起作用,因为一秒是很长的时间,和时钟可能根本不会改变”,我的问题是为什么一秒钟是很长的时间而时钟每秒都会改变,以及作者在什么情况下提到那个时钟不改变?请求通过简单的例子帮助理解。
作者是如何提到即使在几微秒内我们也得不到随机数序列的?
谢谢!
最佳答案
使用随机(或在本例中为伪随机)数字的程序通常在短时间内需要大量的数字。这就是为什么简单地使用时钟并没有真正起作用的原因之一,因为系统时钟的更新速度不如您的代码请求新数字的速度快,因此很可能一遍又一遍地获得相同的结果,直到时钟变化。它在 Unix 系统上可能更引人注目,在该系统中,获取时间的常用方法只能提供秒级精度。甚至微秒也无济于事,因为现在的计算机比微秒快得多。
您要避免的第二个问题是伪随机值的线性相关性。想象一下,您想在一个正方形中随机放置一些点。您将选择一个 x 和一个 y 坐标。如果您的伪随机值是一个简单的线性序列(就像您天真地从时钟中获得的那样),您将得到一条对角线,其中许多点聚集在同一位置。这真的行不通。
最简单的伪随机数生成器类型之一,Linear Congruental Generator有一个类似的问题,尽管乍一看并不那么明显。由于公式非常简单
你仍然会得到相当可预测的结果,尽管只有当你在 3D 空间中选择点时,因为所有数字都位于许多不同的平面上(所有伪随机生成器在特定维度上都会出现这个问题):
关于algorithm - 关于随机数序列生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8517016/
我编写了一个函数来随机从 [-10,10] 中获取一对。 import System.Random main = do { s State g a randomSt = S
好的,我了解如何在 Scala 中实现随机数生成器以及如何设置生成的随机数的上限,但我对如何更改下限感到困惑。例如: var computerGuess= scala.util.Random
我写了一个函数来从 [-10,10] 中随机得到一对。 import System.Random main = do { s State g a randomSt = St
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我正在做一个项目,我需要在其中生成 8 个随机数。由于某种原因,我遇到随机数部分非常耗时的问题。 8 个随机数的意思是我需要一个由数字 0-9 组成的 8 个字符长的字符串。例如 01234567 或
这个问题已经有答案了: Why do I always get the same sequence of random numbers with rand()? (12 个回答) 已关闭 9 年前。
我看到这个问题可能已经在这里得到回答:Random using WELL512 但是,它对用户不太友好,也没有提供如何在“真实世界”的代码片段中使用它的示例。 这是我目前拥有的: #define m
我想知道是否有人可以为我澄清这一行。 Create a function die(x) which rolls a die x times keeping track of how many time
我正在制作一款有 6 名防守球员的足球比赛。我将这段代码设置为随机让他们都向四分卫移动。 我想知道是否有更好的方法来做到这一点。我知道必须有一种方法可以在没有这么多 if 语句的情况下循环它,但我对
在以下位置:http://www.fredosaurus.com/notes-cpp/misc/random.html 它提到如果我们想生成一个1-10范围内的随机数,我们可以这样做: r = (ra
如何在 Linux 和 C++ 中使用随机数? 我找到了一些我想使用的代码,它有一行 srand((unsigned)time(0));//seed 但是 gcc 说 board.cpp:94:24:
这个问题在这里已经有了答案: Generating random whole numbers in JavaScript in a specific range (40 个答案) 关闭 9 年前。
我有以下脚本: Timer=0; function countdown(auctionid){ var auctions; var divs; Timer=Timer+1;
利用oracle的dbms_random包结合rownum来实现,示例如下,随机取499户: select * from ( select * from busi.t_ar_
我需要获取随机数,但它不应该等于之前的数字。这是我的一段代码。但这不起作用。 function getNumber(){ var min = 0; var max = 4; var i;
我对 Haskell 还很陌生。我有一个数据类型: data Sentence= Prop Int | No Sentence | And [Sentence]
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
这个问题已经有答案了: How do I generate random integers within a specific range in Java? (73 个回答) 已关闭 7 年前。
function getRandomArbitrary(min, max) { var r = Math.floor(Math.random() * (max - min + 1) + m
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Generate random number with non-uniform density 我尝试识别/
我是一名优秀的程序员,十分优秀!