简体   繁体   中英

Random numbers from -10 to 10 in C++

How does one make random numbers in the interval -10 to 10 in C++ ?

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

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 . If RAND_MAX%21!=0 lower numbers are generated with higher probability.

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.

Using the Boost Random Library (mentioned by Danvil) all the problems with uniformity of random numbers are eliminated.

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

x = rand() % 21 - 10;

Use the Boost Random Number Library . The built-in random number generator has a notoriously poor distribution quality. Moreover, boost provides you a lot of useful generators.

// 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.

您可以使用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 ) . The example below generates numbers in the interval [-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; 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; ?

Or am I missing something here?

use this will work:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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