为什么使用rand()被认为是不好的? [英] Why is the use of rand() considered bad?

查看:390
本文介绍了为什么使用rand()被认为是不好的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说有些人说rand()的使用是不好的,即使使用srand()来获得种子也是如此.为什么?我想知道事情是怎么发生的...并为另一个问题感到抱歉..但是,这有什么替代方法呢?

I heard some guys telling that the use of rand() is bad EVEN AFTER USING srand() to get a seed. Why is that so? I want to know how the stuff happens... And sorry for another question.. but what is an alternative to this then?

推荐答案

这个故事分为两个部分.

There are two parts to this story.

首先,rand伪随机数生成器.这意味着它取决于种子.对于给定的种子,它将始终给出相同的序列(假定相同的实现).这使其不适用于安全性非常重要的某些应用程序. 但是,这并非特定于rand.这是任何伪随机数生成器的问题.毫无疑问,存在很多类型的问题,其中伪随机数生成器是可以接受的.真正的随机数生成器有其自身的问题(效率,实现,熵),因此对于与安全性无关的问题,通常会使用伪随机数生成器.

First, rand is a pseudorandom number generator. This means it depends on a seed. For a given seed it will always give the same sequence (assuming the same implementation). This makes it not suitable for certain applications where security is of a great concern. But this is not specific to rand. It's a problem of any pseudo-random generator. And there are most certainly a lot of classes of problems where a pseudo-random generator is acceptable. A true random generator has its own problems (efficiency, implementation, entropy) so for problems that are not security related most often a pseudo-random generator is used.

因此,您分析了您的问题,并得出结论,伪随机数生成器是解决方案.在这里,我们讨论了特定于它的C随机库(包括randsrand)的实际问题,并使它过时(又称:您应该永远不会使用rand和C随机库).

So you analyzed your problem and you conclude a pseudo-random generator is the solution. And here we arrive to the real problems of the C random library (which includes rand and srand) who are specific to it and make it obsolete (a.k.a.: the reasons you should never use rand and the C random library).

  • 一个问题是它具有全局状态(由srand设置).这使得不可能同时使用多个随机引擎.它还使多线程任务变得非常复杂.

  • One problem is that it has a global state (set by srand). This makes it impossible to use multiple random engines at the same time. It also greatly complicates multithreaded tasks.

其中最明显的问题是它缺少分发引擎:rand为您提供间隔为[0 RAND_MAX]的数字.在此间隔中它是统一的,这意味着此间隔中的每个数字都有相同的出现概率.但是大多数情况下,您需要在特定时间间隔内使用随机数.假设[0, 1017].一个常用的(天真)公式是rand() % 1018.但这是一个问题,除非RAND_MAX1018的精确倍数,否则您将不会获得均匀的分布.

The most visible problem of it is that it lacks a distribution engine: rand gives you a number in interval [0 RAND_MAX]. It is uniform in this interval, which means that each number in this interval has the same probability to appear. But most often you need a random number in a specific interval. Let's say [0, 1017]. A commonly (and naive) used formula is rand() % 1018. But the problem with this is that unless RAND_MAX is an exact multiple of 1018 you won't get an uniform distribution.

另一个问题是rand的实现质量.这里还有其他答案比我更详细地说明了这一点,因此请阅读它们.

Another problem is the Quality of Implementation of rand. There are other answers here detailing this better than I could, so please read them.

在现代C ++中,您绝对应该使用<random>中的C ++库,该库带有多个随机定义的引擎以及整数和浮点类型的各种分布.

In modern C++ you should definitely use the C++ library from <random> which comes with multiple random well-defined engines and various distributions for integer and floating point types.

这篇关于为什么使用rand()被认为是不好的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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