如何在C中随机生成0或1 [英] How to generate either 0 or 1 randomly in C

查看:59
本文介绍了如何在C中随机生成0或1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于这个主题的帖子:

兰德如何() 工作?它有某些倾向吗?有什么更好用的吗?

C 中的随机数生成器是如何工作的

这就是我得到的:

1) xn+1 取决于 xn,即之前生成的随机数.

2) 不建议在程序中多次初始化种子.

3) 使用 rand()%2 随机生成 0 或 1 是一种不好的做法.

我的问题是:

1) 有没有我错过的其他库可以在不依赖于先前输出的情况下生成完全随机数(0 或 1)?

2) 如果使用内置的 rand() 函数来满足要求,是否还有其他解决方法?

3) 在一个程序中多次初始化种子有什么副作用?

代码片段:

srand(time(NULL));d1=rand()%2;d2=rand()%2;

这里我的意图是让 d1 和 d2 完全相互独立.

我最初的想法是这样做:

srand(time(NULL));d1=rand()%2;srand(时间(空));d2=rand()%2;

但正如我之前提到的,这是基于其他帖子的,我认为这是一种不好的做法吗?

那么,有人可以回答上述问题吗?如果我完全错过了明显的事情,我深表歉意.

  1. 是否有其他库让我错过了生成 0 到 1 之间的完全随机数而不依赖于以前的输出?

不在标准 C 库中.还有很多其他库可以生成更好的"伪随机数.

<块引用>

  1. 是否还有其他解决方法可以使用内置的 rand() 函数来满足要求?

rand 的大多数标准库实现都会生成随机数序列,其中低位具有短序列和/或彼此之间并不像人们希望的那样独立.高位通常分布得更好.因此,使用标准库 rand 函数生成随机单个位(0 或 1)的更好方法是:

(rand() > RAND_MAX/2)

或使用内部位:

(rand() & 0x400U != 0)

那些将产生与大多数标准库rand 实现合理不相关的序列,并且不会产生比检查低位更多的计算开销.如果这对您来说还不够好,您可能需要研究其他伪随机数生成器.

所有这些(包括 rand() % 2)都假设 RAND_MAX 是奇数,这几乎总是如此.(如果 RAND_MAX 是偶数,则可能值的数量可能为奇数,并且将奇数个可能值划分为两个阵营的任何方式都必须略有偏差.)

<块引用>

  1. 在程序中多次初始化种子有什么副作用?

您应该将随机数生成器视为在播种后生成不是很随机"的数字,随着您连续生成新的随机数,质量会提高.请记住,如果您使用一些种子为随机数生成器播种,您将获得完全相同的序列,就像您下次使用相同的种子为生成器播种时一样.(由于 time() 返回的是秒数,因此快速连续的两次连续调用通常会产生完全相同的数字,或者偶尔会产生两个连续的数字.但绝对不是两个随机不相关的数字.)>

因此,重新播种的副作用是您获得的随机数较少,而且可能与上次重新播种时获得的随机数完全相同.

I have read so many posts on this topic:

How does rand() work? Does it have certain tendencies? Is there something better to use?

How does the random number generator work in C

and this is what I got:

1) xn+1 depends on xn i.e., previous random number that is generated.

2) It is not recommended to initialize the seed more than once in the program.

3) It is a bad practice to use rand()%2 to generate either 0 or 1 randomly.

My questions are:

1) Are there any other libraries that I missed to take a look to generate a completely random number (either 0 or 1) without depending on previous output?

2) If there is any other work around using the inbuilt rand() function to satisfy the requirement?

3) What is the side effect of initializing the seed more than once in a program?

Code snippet:

srand(time(NULL));
d1=rand()%2;
d2=rand()%2;

Here my intention is to make d1 and d2 completely independent of each other.

My initial thought is to do this:

srand(time(NULL));
d1=rand()%2;
srand(time(NULL));
d2=rand()%2;

But as I mentioned earlier which is based on other posts, this is a bad practice I suppose?

So, can anyone please answer the above questions? I apologize if I completely missed an obvious thing.

解决方案

  1. Are there any other libraries that I missed to take a look to generate a completely random number between 0 and 1 without depending on previous output?

Not in the standard C library. There are lots of other libraries which generate "better" pseudo-random numbers.

  1. If there is any other work around using the inbuilt rand() function to satisfy the requirement?

Most standard library implementations of rand produce sequences of random numbers where the low-order bit(s) have a short sequence and/or are not as independent of each other as one would like. The high-order bits are generally better distributed. So a better way of using the standard library rand function to generate a random single bit (0 or 1) is:

(rand() > RAND_MAX / 2)

or use an interior bit:

(rand() & 0x400U != 0)

Those will produce reasonably uncorrelated sequences with most standard library rand implementations, and impose no more computational overhead than checking the low-order bit. If that's not good enough for you, you'll probably want to research other pseudo-random number generators.

All of these (including rand() % 2) assume that RAND_MAX is odd, which is almost always the case. (If RAND_MAX were even, there would be an odd number of possible values and any way of dividing an odd number of possible values into two camps must be slightly biased.)

  1. What is the side effect of initializing the seed more than once in a program?

You should think of the random number generator as producing "not very random" numbers after being seeded, with the quality improving as you successively generate new random numbers. And remember that if you seed the random number generator using some seed, you will get exactly the same sequence as you will the next time you seed the generator with the same seed. (Since time() returns a number of seconds, two successive calls in quick succession will usually produce exactly the same number, or very occasionally two consecutive numbers. But definitely not two random uncorrelated numbers.)

So the side effect of reseeding is that you get less random numbers, and possibly exactly the same ones as you got the last time you reseeded.

这篇关于如何在C中随机生成0或1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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