为什么C ++兰特()似乎产生数量级的顺序相同的只有数字? [英] Why does C++ rand() seem to generate only numbers of the same order of magnitude?

查看:124
本文介绍了为什么C ++兰特()似乎产生数量级的顺序相同的只有数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用C / C ++,我现在面临的兰特功能,也许是种子有问题的小应用程序:

In a small application written in C/C++, I am facing a problem with the rand function and maybe the seed :

欲产生随机数的不同阶的,即具有不同的对数值(基数为2)的序列。但似乎产生的所有数字都以相同的顺序的,波动只是之间2 ^ 25和2 ^ 30

I want to produce a sequence of random numbers that are of different orders, i.e. with different logarithm values (base 2). But it seems that all the numbers produced are of the same order, fluctuating just between 2^25 and 2^30.

是因为兰特()与Unix时间播种这是现在一个比较大的数字?什么我忘了?
我在开始时播种兰特()只有当的main()

Is it because rand() is seeded with Unix time which is by now a relatively big number? What am I forgetting ? I am seeding rand() only once at the beginning of the main().

推荐答案

目前只有3个1和2 30 这是不与2 25 之间的数字% 2 30 。所以,这听起来pretty正常:)

There are only 3% of numbers between 1 and 230 which are NOT between 225 and 230. So, this sounds pretty normal :)

由于2 25 / 2 30 = 2 -5 = 1/32 = 0.03125 = 3.125%

Because 225 / 230 = 2-5 = 1/32 = 0.03125 = 3.125%

这篇关于为什么C ++兰特()似乎产生数量级的顺序相同的只有数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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