总是反复兰德公司提供的数字() [英] Always repeated numbers given by rand()

查看:91
本文介绍了总是反复兰德公司提供的数字()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一看使用兰特(),它总是给我相同的值多次在一排。我试过循环之前使用函数srand(时间(NULL)),但它并不能帮助...

I use rand() in a look, and it always give me the same values several times in a row. I tried to use srand(time(NULL)) before the loop, but it does not help...

推荐答案

通常情况下,你得到重复数字的唯一原因是,如果你使用函数srand 使用相同的种子值在循环(和时间(0)将会的给你一个紧密循环相同的值)。

Usually, the only reason you get repeated numbers is if you use srand within the loop with the same seed value (and time(0) will give you the same value in a tight loop).

当然,一个真正的随机数序列的可以的给你重复的数字。即使是一个不这样做可以反复给你同样的号码,如果你是严重操纵它。

Of course, a true random number sequence can give you repeated numbers. Even one that doesn't do that can give you the same number repeatedly if you're manipulating it badly.

例如,兰特()/ 100000 可能是做一个不那么好的事情,如果算法往往有利于在返回值的低端变化因为 100000,164534,186410,199999 会给你 1 兰特()序,1个,1个,1 兰特()%100000 很可能在这种情况下一个更好的选择,如果连续数之间的变化是你的价值)。

For example, rand() / 100000 may be a not-so-good thing to do if the algorithm tends to favour changes at the low end of the returned value since the rand() sequence of 100000, 164534, 186410, 199999 will give you 1, 1, 1, 1 (rand() % 100000 may well be a better choice in that case if changes between consecutive number is what you value).

当然,没有看到实际的code,猜测一样,可能是我们能做到的最好。你最好的一点,因为大多数问题报告,是提供表现出这个问题的小完整的示例。

Of course, without seeing your actual code, guesses like that are probably the best we can do. Your best bit, as with most problem reports, is to provide a small complete sample that exhibits the problem.

这篇关于总是反复兰德公司提供的数字()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆