简体   繁体   English

C ++中-10到10的随机数

[英]Random numbers from -10 to 10 in C++

How does one make random numbers in the interval -10 to 10 in C++ ? 如何在C ++的-10到10区间内制作随机数?

srand(int(time(0)));//seed
for(int i  = 0; i < size; i++){
 myArray[i] = 1 + rand()  % 20 - 10;//this will give from -9 to 10
 myArray2[i] =rand()  % 20 - 10;//and this will -10 to 9
}

To get uniform distribution you must divide with RAND_MAX first 要获得统一分布,必须首先除以RAND_MAX

static_cast<int>(21*static_cast<double>(rand())/(RAND_MAX+1)) - 10

using 运用

rand() % 21 - 10;

is faster and is often used in applications but the resulted distribution is not uniform. 更快,常用于应用程序,但结果分布不均匀。 Function rand() generates numbers from from 0 to RAND_MAX . 函数rand()生成从0RAND_MAX数字。 If RAND_MAX%21!=0 lower numbers are generated with higher probability. 如果RAND_MAX%21!=0则以较高的概率生成较低的数字。

You may also consider to use the modulo method but with dropping of some of the random numbers: 您也可以考虑使用模数方法,但删除一些随机数:

int randMax = RAND_MAX - RAND_MAX%21;

int p=RAND_MAX+1;
while(p>randMax)
        p=rand();

x=p%21 - 10;

Edit (comments from Johannes and Steve): 编辑(约翰内斯和史蒂夫的评论):

When dividing with RAND_MAX there are some numbers from the range which will be picked more often so the proper way to handle is to reject numbers which would lead to an uneven distribution on the target interval. 当用RAND_MAX分割时,从该范围中有一些数字将被更频繁地挑选,因此正确的处理方法是拒绝会导致目标间隔上的不均匀分布的数字。

Using the Boost Random Library (mentioned by Danvil) all the problems with uniformity of random numbers are eliminated. 使用Boost Random Library(Danvil提到),消除了随机数均匀性的所有问题。

你需要21的范围,而不是20,所以做这样的事情:

x = rand() % 21 - 10;

Use the Boost Random Number Library . 使用Boost随机数库 The built-in random number generator has a notoriously poor distribution quality. 内置随机数发生器的分配质量差。 Moreover, boost provides you a lot of useful generators. 此外,boost为您提供了许多有用的生成器。

// based on boost random_demo.cpp profane demo
#include <iostream>

#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/variate_generator.hpp>

int main() {
  boost::mt19937 gen(42u); // seed generator
  boost::uniform_int<> uni_dist(-10, 10); // random int from -10 to 10 inclusive
  boost::variate_generator<boost::mt19937&, boost::uniform_int<> > 
    uni(gen, uni_dist); // callable

  for(int i = 0; i < 10; i++)
    std::cout << uni() << ' ';
}

Output: 输出:

-3 6 9 -7 5 6 2 2 -7 -1 

Note from the future: This is built-in in C++11 now. 未来的注意事项:现在已经在C ++ 11中内置了。

您可以使用rand() % 21生成[0,20]之间的随机数,然后从每个生成的数字中减去10

Using C++11's random library this is much simpler and less error prone( see rand() Considered Harmful presentation and slides for more details ) . 使用C ++ 11的random库,这更简单,更不容易出错( 请参阅rand()有关详细信息,请参阅有害的演示文稿幻灯片 )。 The example below generates numbers in the interval [-10,10] : 以下示例在区间[-10,10]中生成数字:

#include <iostream>
#include <random>

int main()
{
    std::random_device rd;

    std::mt19937 e2(rd());

    std::uniform_int_distribution<int> dist(-10, 10);

    for (int n = 0; n < 10; ++n) {
            std::cout << dist(e2) << ", " ;
    }
    std::cout << std::endl ;
}

您可以使用Knuth的减法随机数生成器在(0,1)中生成一个数字'u',然后使用这个简单的线性方程在[-10,10]中得到一个随机数:

-10*u + (1-u)*10

You've got a fencepost error -- the range you're interested in is one larger than the modulo you were using; 你有一个fencepost错误 - 你感兴趣的范围比你使用的模数大一个; instead try: 而是尝试:

myArray2[i] =rand()  % 21 - 10;//and this will -10 to +10

rand() % 21 - 10

如果您希望数字在[-10,10]范围内,那么您有21个可能的数字。

(rand() % 21) - 10;

How about (rand() % 21) - 10; 怎么样(rand()%21) - 10; ?

Or am I missing something here? 或者我在这里遗漏了什么?

use this will work: 使用这将工作:

int x = (rand() % 21) - 10;
cout<<x;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM