Generating a random double between a range of values - c++

Im currently having trouble generating random numbers between -32.768 and 32.768. It keeps giving me the same values but with a small change in the decimal field. ex : 27.xxx.
Heres my code, any help would be appreciated.
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
srand( time(NULL) );
double r = (68.556*rand()/RAND_MAX - 32.768);
cout << r << endl;
return 0;
}

I should mention if you're using a C++11 compiler, you can use something like this, which is actually easier to read and harder to mess up:
#include <random>
#include <iostream>
#include <ctime>
int main()
{
//Type of random number distribution
std::uniform_real_distribution<double> dist(-32.768, 32.768); //(min, max)
//Mersenne Twister: Good quality random number generator
std::mt19937 rng;
//Initialize with non-deterministic seeds
rng.seed(std::random_device{}());
// generate 10 random numbers.
for (int i=0; i<10; i++)
{
std::cout << dist(rng) << std::endl;
}
return 0;
}
As bames53 pointed out, the above code can be made even shorter if you make full use of c++11:
#include <random>
#include <iostream>
#include <ctime>
#include <algorithm>
#include <iterator>
int main()
{
std::mt19937 rng;
std::uniform_real_distribution<double> dist(-32.768, 32.768); //(min, max)
rng.seed(std::random_device{}()); //non-deterministic seed
std::generate_n(
std::ostream_iterator<double>(std::cout, "\n"),
10,
[&]{ return dist(rng);} );
return 0;
}

Also, If you are not using c++ 11 you can use the following function instead:
double randDouble(double precision, double lowerBound, double upperBound) {
double random;
random = static_cast<double>(((rand()%(static_cast<int>(std::pow(10,precision)*(upperBound - lowerBound) + 1))) + lowerBound*std::pow(10,precision)))/std::pow(10,precision);
return random;
}

So, I think this is a typical case of "using time(NULL) isn't a great way of seeding random numbers for runs that start close together". There isn't that many bits that change in time(NULL) from one call to the next, so random numbers are fairly similar. This is not a new phenomena - if you google "my random numbers aren't very random", you'll find LOTS of this.
There are a few different solutions - getting a microsecond or nanosecond time would be the simplest choice - in Linux gettimeofday will give you a microsecond time as part of the struct.

It seams to be plainly obvious but some of the examples say otherwise... but i thought when you divide 1 int with another you always get an int? and you need to type cast each int to double/float before you divide them.
ie: double r = (68.556* (double)rand()/(double)RAND_MAX - 32.768);
also if you call srand() every time you call rand() you reset the seed which results in similar values returned every time instead of ''random'' ones.

I've added a for loop to your program:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main () {
srand(time (NULL));
for (int i = 0; i < 10; ++i) {
double r = ((68.556 * rand () / RAND_MAX) - 32.768);
cout << r << endl;
}
return 0;
}
Example output:
31.6779
-28.2096
31.5672
18.9916
-1.57149
-0.993889
-32.4737
24.6982
25.936
26.4152
It seems Okay to me. I've added the code on Ideone for you.
Here are four runs:
Run 1:
-29.0863
-22.3973
34.1034
-1.41155
-2.60232
-30.5257
31.9254
-17.0673
31.7522
28.227
Run 2:
-14.2872
-0.185124
-27.3674
8.12921
22.4611
-0.414546
-21.4944
-11.0871
4.87673
5.4545
Run 3:
-23.9083
-6.04738
-6.54314
30.1767
-16.2224
-19.4619
3.37444
9.28014
25.9318
-22.8807
Run 4:
25.1364
16.3011
0.596151
5.3953
-25.2851
10.7301
18.4541
-18.8511
-0.828694
22.8335
Perhaps you're not waiting at least a second between runs?

Related

How to generate different random number for the same variable

I want to use a while loop to generate a random number for a variable to spell out a scrambled word. My problem is that my code generates a number that is random but repeats that number rather than using a new number.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
string wordList[5] = {"cool", "friend", "helpful", "amazing",
"person"};
srand(time(0));
int rWord = rand() % 5 + 1;
string randWord = wordList[rWord];
int runs = 0;
int wordLen = randWord.length();
while(runs != wordLen){
int ranLN = rand() % wordLen;
char randLetter = randWord[ranLN];
cout << randLetter;
runs++;
}
return 0;
}
I expected my results to be a fully scrambled word, but I instead got repeated letters. For example, I got the word "friend" scrambled as "eennn".
As suggested in comments, the current range of rWord is 1,2,3,4,5 which must be fixed to 0,1,2,3,4.
Thus I removed +1 from it's initialization equation in the following answer.
In addition, ranLN can be duplicate thus you got repeated letters.
Then, a possible way is recursively shuffling all characters of randWord and output them after the while loop finished as follows.
The same algorithm is shown here as an example:
DEMO
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <utility>
int main()
{
std::string wordList[5] = {"cool", "friend", "helpful", "amazing", "person"};
srand(time(0));
std::size_t rWord = rand() % 5;
std::string randWord = wordList[rWord];
std::size_t runs = 0;
std::size_t wordLen = randWord.length();
while(runs != wordLen)
{
std::swap(randWord[runs], randWord[rand() % wordLen]);
++runs;
}
std::cout << randWord << std::endl;
return 0;
}
BTW, although rand() should be usually implemented by a something better LCG,
but, for instance as noted in (my local) C++ standard draft n4687, the algorithm used in rand() is completely compiler implementation defined:
29.6.9 Low-quality random number generation [c.math.rand]
int rand();
void srand(unsigned int seed);
... rand’s underlying algorithm is unspecified. Use of rand therefore continues to be non-portable, with unpredictable and oft-questionable quality and performance.
Fortunately, in C++11 and over, we can use <random> to generate a guaranteed quality randomness.
Thus I recommend you to use them with std::shuffle as follows.
If you need more high-quality randomness, you can use std::mt19937 instead of std::minstd_rand:
DEMO
#include <iostream>
#include <string>
#include <random>
#include <algorithm>
int main()
{
std::string wordList[5] = {"cool", "friend", "helpful", "amazing", "person"};
std::minstd_rand gen(std::random_device{}());
std::uniform_int_distribution<std::size_t> dis(0, 4);
std::size_t rWord = dis(gen);
std::string randWord = wordList[rWord];
std::shuffle(randWord.begin(), randWord.end(), gen);
std::cout << randWord << std::endl;
return 0;
}
In my humble opinion after generating all random words then using set data structure would make the random word unique.

Random number generator producing identical results

I am having trouble using the random header to create a simple random number generator.
#include <iostream>
#include <random>
using namespace std;
int main()
{
random_device rd; //seed generator
mt19937_64 generator{rd()}; //generator initialized with seed from rd
uniform_int_distribution<> dist{1, 6};
for(int i = 0; i < 15; i++)
{
int random = dist(generator);
cout << random << endl;
}
}
This code produces identical results every time I run the program. What am I doing wrong? Also is there a way to modify this code such that it will generate a floating point number between 0 and 1? I don't think the uniform_int_distribution will let me and I can't figure out which distribution to use.
EDIT: Posted a possible solution to my problem below
Here is what I came up with eventually:
#include <iostream>
#include <ctime>
#include <random>
using namespace std;
int main()
{
srand(time(0));
default_random_engine rd(rand());
mt19937_64 generator{rd()}; //generator initialized with seed from rd
uniform_real_distribution<double> dist{0,1};
for(int i = 0; i < 15; i++)
{
double random = dist(generator);
cout << fixed << random << endl;
}
}
It turns out that you actually CAN combine srand(time(0)) with an engine from the random header file, and their powers combined seem to produce random-feeling numbers better than I have managed with either alone. Please feel free to point out any problems with this arrangement.

generate random numbers and put them in an array

I'm looking to write a little dice game called Farkle (you may know it from Kingdom come delivarance) in C++ but I'm still learning, so I have some trouble with it.
atm I'm trying to roll 6 dice and put every rolled number in an array to be able to work with it afterwards. everything seem to work fine but Visual Studio ist outputting this error Code:
Run-Time Check Failure #2 - Stack around the variable 'die' was corrupted.
this is my code:
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
void dice() {
int die[5];
int i = 1;
while (i <= 6) {
die[i] = rand() % 6 + 1;
cout << die[i];
i++;
}
}
int main()
{
srand(time(NULL));
dice();
system("STOP");
return 0;
}
is ths actually the right approach for this kind of programm?
No, a better way to generate uniformly distributed random numbers would be
#include <random>
#include <algorithm>
std::random_device rd; //Will be used to obtain a seed for the random number engine
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
std::uniform_int_distribution<> d6(1, 6); // {1, 2, 3, 4, 5, 6} with equal probability
int die[5];
std::generate(die, die + 5, [&gen, &d6](){ return d6(gen); });
If you were generating multiple sets of 5d6, you can re-use the same gen rather than re-initialising it each time
As others pointed out. Your error stems from using a too small array. This post will be more about your code being more like C.
It is more idiomatic in C++ to use std::array instead of raw arrays.
Also it is recommended not to use rand() since it produces bad random numbers and by using the modulo operation you are introducing additional bias to you random numbers. Instead one should use the classes from the <random> header.
To make the code even more readable you could try to use the functions from the <algorithm> to replace you loops by named algorithms.
This leads to following code:
#include <algorithm>
#include <array>
#include <iostream>
#include <iterator>
#include <random>
void dice() {
std::array<int, 6> die;
std::mt19937 gen{std::random_device{}()};
std::uniform_int_distribution<int> dice_roll{1, 6};
std::generate(begin(die), end(die), [&] { return dice_roll(gen); });
std::copy(begin(die), end(die), std::ostream_iterator<int>{std::cout});
}
int main() {
dice();
std::cin.get();
}
You have 2 problems in your code:
The size of your array is 5, but you access 6 indices (1 to 6), you can avoid this by changing the <= to < in the condition.
The indices of an array in C++ start with 0, but you start with 1. You can fix that if you change each die[i] to die[i-1] in your code.
Another approach (fixing both problems) would be to initialize i=0 and go with while (i < 5)
index i should be from 0 to 5, not 1 to 6.
It's obvious that when i = 6, it run out of the range of dice which made an error.
Edit these lines:
int i = 0;
while (i <= 5) {
....
Try this code :
#include <iostream>
#include <cstdlib>
#include <ctime>
void populateArray( int ar[], /*const*/ int n )
{
for( int i = 0 ; i < n ; ++i ) ar[i] = std::rand() % 50 + 1 ;
}
int main()
{
// http://en.cppreference.com/w/cpp/numeric/random/srand
std::srand( std::time(nullptr) ) ; // **** important ****
const int ARRAY_SIZE = 50;
int ar[ARRAY_SIZE] = {0} ;
populateArray( ar, ARRAY_SIZE ) ;
for( int v : ar ) std::cout << v << ' ' ;
}

Duplicate values generated by mt19937

I am working with C++11's random library, and I have a small program that generates a coordinate pair x, y on a circle with unit radius. Here is the simple multithreaded program
#include <iostream>
#include <fstream>
#include <random>
using namespace std;
int main()
{
const double PI = 3.1415;
double angle, radius, X, Y;
int i;
vector<double> finalPositionX, finalPositionY;
#pragma omp parallel
{
vector <double> positionX, positionY;
mt19937 engine(0);
uniform_real_distribution<> uniform(0, 1);
normal_distribution<double> normal(0, 1);
#pragma omp for private(angle, radius, X, Y)
for(i=0; i<1000000; ++i)
{
angle = uniform(engine)*2.0*PI;
radius = sqrt(uniform(engine));
X = radius*cos(angle);
Y = radius*sin(angle);
positionX.push_back(X);
positionY.push_back(Y);
}
#pragma omp barrier
#pragma omp critical
finalPositionX.insert(finalPositionX.end(), positionX.begin(), positionX.end());
finalPositionY.insert(finalPositionY.end(), positionY.begin(), positionY.end());
}
ofstream output_data("positions.txt", ios::out);
output_data.precision(9);
for(unsigned long long temp_var=0; temp_var<(unsigned long long)finalPositionX.size(); temp_var++)
{
output_data << finalPositionX[temp_var]
<< "\t\t\t\t"
<< finalPositionY[temp_var]
<< "\n";
}
output_data.close();
return 0;
}
Question: Many of the x-coordinates appear twice (same with y-coordinates). I don't understand this, since the period of the mt19937 is much longer than 1.000.000. Does anyone have an idea of what is wrong here?
Note: I get the same behavior when I don't multithread the application, so the problem is not related to wrong multithreading.
EDIT As pointed out in one of the answers, I shouldn't use the same seed for both threads - but that is an error I made when formulating this question, in my real program I seem the threads differently.
Using the core part of your code, I wrote this imperfect test but from what I can see the distribution is pretty uniform:
#include <iostream>
#include <fstream>
#include <random>
#include <map>
#include <iomanip>
using namespace std;
int main()
{
int i;
vector<double> finalPositionX, finalPositionY;
std::map<int, int> hist;
vector <double> positionX, positionY;
mt19937 engine(0);
uniform_real_distribution<> uniform(0, 1);
//normal_distribution<double> normal(0, 1);
for(i=0; i<1000000; ++i)
{
double rnum = uniform(engine);
++hist[std::round(1000*rnum)];
}
for (auto p : hist) {
std::cout << std::fixed << std::setprecision(1) << std::setw(2)
<< p.first << ' ' << std::string(p.second/200, '*') << '\n';
}
return 0;
}
and as others already said it is not unexpected to see some values repeated. For the normal distribution, I used the following modification to rnum and hist to test that and it looks good too:
double rnum = normal(engine);
++hist[std::round(10*rnum)];
As described in this article (and a later article by a Stack Overflow contributor), true randomness doesn't distribute perfectly.
Good randomness :
Bad randomness :
I really recommend reading the article, but to summarize it: a RNG has to be unpredictable, which implies that calling it 100 times must not perfectly fill a 10x10 grid.
First of all - just because you get the same number twice doesn't mean it isn't random. If you throw a dice six times, would you expect six different results? See birthday paradox. That being said - you are right that you shouldn't see too much repetition in this particular case.
I'm not familiar with "#pragma omp parallel", but my guess is you are spawning multiple threads that all seed the mt19937 with the same seed (0). You should use different seeds for all threads - e.g. the thread id.

What's wrong with my random number generator?

I'm just diving into some C++ and I decided to make a random number generator (how random the number is, it really doesn't matter). Most of the code is copied off then net but my newbie eyes cannot see anything wrong with this, is there any way this can be tweaked to give a number other than "6" each time?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int random_number(int min, int max)
{
srand((unsigned)time(0));
int random_num;
int range=(max-min)+1;
random_num = min+int(range*rand()/(RAND_MAX + 1.0));
return random_num;
}
int main()
{
for(int i =0;i < 100;i++)
{
cout << random_number(3,10) << endl;
}
}
Add srand before the loop
srand((unsigned)time(0));
for(int i =0;i < 100;i++)
{
std::cout << random_number(3,10) << endl;
}
Don't call srand() within random_number(). This will re-seed the random number generator every call. For 100 calls, you'll very likely get the same seed every call, and therefore the same number.
The problem is that you use srand everytime. CPU is so fast that it will execute all this code in a single second, so you get the same seed each time.
Move srand out of the loop, call it only once.