久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

每次循環(huán)迭代相同的隨機(jī)數(shù)

Same random numbers every loop iteration(每次循環(huán)迭代相同的隨機(jī)數(shù))
本文介紹了每次循環(huán)迭代相同的隨機(jī)數(shù)的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有一個(gè)運(yùn)行 15 次的 for 循環(huán),每次迭代都使用 dh.setDoors().

I have a for loop that runs 15 times, with dh.setDoors() in every iteration.

setDoors 所做的是調(diào)用 srand(time(0)),然后每當(dāng)需要隨機(jī)數(shù)時(shí)它都會(huì)使用,例如 carSetter =rand()%3+1.或者,它可以使用 decider = rand()%2+1.

What setDoors does is call srand(time(0)), then whenever a random number is needed it'll use, for example, carSetter = rand()%3+1. Alternatively, it may use decider = rand()%2+1.

現(xiàn)在,通常decidercarSetter以不同的方式使用,但我懷疑有問題并打印出carSetterdecider 在每次迭代中.這是結(jié)果:

Now, normally decider and carSetter are used in a different ways, but I suspected a problem and made it print out carSetter and decider at every iteration. Here's what came out:

Door 1 has car
Decider is 2
Door 1 has car
Decider is 2
Door 1 has car
Decider is 2
Door 1 has car
Decider is 2
Door 1 has car
Decider is 2
etc...

當(dāng)我多次運(yùn)行它時(shí),值1"和2"會(huì)發(fā)生變化,但在 15 次中仍然相同.

The values '1' and '2' change when I run it multiple times, but are still the same throughout the 15 times.

既然循環(huán)運(yùn)行了 15 次不同的時(shí)間,難道 carSetterdecider 不應(yīng)該每次迭代都打印出不同的隨機(jī)數(shù)嗎?

Since the loop is running 15 different times, shouldn't carSetter and decider print out a different random number every iteration?

當(dāng)我沒有 srand(time(0)) 時(shí),它按預(yù)期工作,但是沒有種子集,所以每次都是相同的隨機(jī)"數(shù)字序列,所以它是可能是種子有問題?

When I don't have srand(time(0)), it works as expected, but there's no seed set, so it's the same sequence of "random" numbers each time, so it's probably a problem with the seed?

推薦答案

當(dāng)你調(diào)用 srand(x) 時(shí),那么 x 的值決定了偽序列在對(duì) rand() 的后續(xù)調(diào)用中返回的隨機(jī)數(shù),完全取決于 x 的值.

When you call srand(x), then the value of x determines the sequence of pseudo-random numbers returned in following calls to rand(), depending entirely on the value of x.

當(dāng)您處于循環(huán)中并在頂部調(diào)用 srand() 時(shí):

When you're in a loop and call srand() at the top:

while (...) {
    srand(time(0));
    x = rand();
    y = rand();
}

然后根據(jù)time(0)返回的值生成相同隨機(jī)數(shù)序列.由于計(jì)算機(jī)速度很快,而且您的循環(huán)可能會(huì)在不到一秒的時(shí)間內(nèi)運(yùn)行,time(0) 每次通過循環(huán)都會(huì)返回 相同 值.所以 xy 每次迭代都是一樣的.

then the same random number sequence is generated depending on the value that time(0) returns. Since computers are fast and your loop probably runs in less than a second, time(0) returns the same value each time through the loop. So x and y will be the same each iteration.

相反,您通常只需要在程序開始時(shí)調(diào)用 srand() 一次:

Instead, you only usually need to call srand() once at the start of your program:

srand(time(0));

while (...) {
    x = rand();
    y = rand();
}

在上面的例子中,xy 每次循環(huán)都會(huì)有不同的值.

In the above case, x and y will have different values each time through the loop.

這篇關(guān)于每次循環(huán)迭代相同的隨機(jī)數(shù)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

What do compilers do with compile-time branching?(編譯器如何處理編譯時(shí)分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 嗎?)
Checking for NULL pointer in C/C++(在 C/C++ 中檢查空指針)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比較運(yùn)算符的數(shù)學(xué)式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區(qū)別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達(dá)式中的變量聲明)
主站蜘蛛池模板: 国产在线播| 欧美精品久久 | 成人免费一区二区三区视频网站 | 天堂网中文字幕在线观看 | 天天草天天 | 精品毛片| www.久久| 羞羞网站在线免费观看 | 在线观看视频一区二区三区 | 99精品欧美一区二区蜜桃免费 | 亚洲精品久久久久久久久久久 | 国产成人叼嘿视频在线观看 | 久久丝袜视频 | 国产伦精品一区二区三区照片91 | 国产欧美在线观看 | 欧美日韩视频在线播放 | 91精品麻豆日日躁夜夜躁 | 国产精品99精品久久免费 | 日韩2020狼一二三 | 国产精品久久久久久久久久久久久 | 成人在线视频免费观看 | 亚洲一区二区电影网 | 91精品国产一区二区在线观看 | 亚洲成人免费视频在线观看 | 中文字幕不卡视频在线观看 | 国产精品久久片 | 毛片com| 综合久久综合久久 | 91免费观看国产 | 国产精品永久免费视频 | 免费a级毛片在线播放 | 超碰97免费 | 色综合天天天天做夜夜夜夜做 | 免费在线a视频 | 日本精品视频一区二区 | 亚洲第一网站 | 伊伊综合网| 欧美电影网| 亚洲一区二区在线电影 | 中文字幕亚洲精品 | 中文字幕一区二区三区在线乱码 |