How does one make random numbers in the interval -10 to 10 in C++ ?
srand(int(time(0)));//seed
for(int i开发者_JAVA百科 = 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.
You need a range of 21, not 20, so do something like this:
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.
You can generate random numbers between [0,20]
using rand() % 21
and then subtract 10
from every generated number.
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 ;
}
You could use Knuth's subtractive random number generator to generate a number, 'u' in (0,1) and then use this simple linear equation to get a random number in [-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
If you want the numbers to be in the range [-10, 10], then you have 21 possible numbers.
(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;
精彩评论