Random double generation

Mar 2, 2018 at 3:26am
I'm trying to generate a random double between -1 and 1, should have 2 significant figures i.e. 0.23, here is what I have so far but it repeats the same number over and over.

1
2
3
4
5
6
  double fRand()
{
    srand(time(0));
    double a = rand() % 201 - 100;
    return a / 100.0;
}
Mar 2, 2018 at 3:54am
Generate a random integer between -100 and +100 and divide by 100.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <random>

double rand_dbl()
{
    // http://en.cppreference.com/w/cpp/numeric/random
    static std::mt19937 rng( std::random_device{}() ) ; ;
    static std::uniform_int_distribution<int> distrib( -100, +100 ) ;

    return distrib(rng) / 100.0 ;
}

int main()
{
    for( int i = 0 ; i < 25 ; ++i ) std::cout << std::showpos << rand_dbl() << ' ' ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/c1b1e794547fc75a
Mar 2, 2018 at 7:31am
If you're using std::uniform_int_distribution you could just as well use std::uniform_real_distribution to generate a double directly.

1
2
3
4
5
6
7
8
double rand_dbl()
{
    // http://en.cppreference.com/w/cpp/numeric/random
    static std::mt19937 rng( std::random_device{}() ) ; ;
    static std::uniform_real_distribution<double> distrib( -1.0, +1.0 ) ;

    return distrib(rng);
}
Mar 2, 2018 at 7:39am
I think it won't meet this requirement (without additional rounding): should have 2 significant figures i.e. 0.23
ie. generate one of the 201 possible floating point values.
Mar 2, 2018 at 8:09am
Ah, sorry. I missed that part.
Mar 2, 2018 at 9:56am
it repeats the same number over and over.

You do call srand() with current time. If the timer has low resolution and you call fRand() rapidly, then current time remains same on every call.
Same time == same seed => same position in the deterministic sequence from which the rand() picks numbers from.

In other words, calling srand() more than once in your program is the likely cause of repeats.


Overall, the rand() and srand() have been deprecated; scheduled for removal. It is not recommended to use them in new programs. One has to learn the <random> if one needs randomness in the future, so why not start now?
Mar 2, 2018 at 1:24pm
the rand() and srand() have been deprecated; scheduled for removal

I don't think it has been officially deprecated yet, only discouraged.

Note on rand() from the C++17 standard
The other random number generation facilities in this International Standard (29.6) are often preferable to rand, because rand’s underlying algorithm is unspecified. Use of rand therefore continues to be non-portable, with unpredictable and oft-questionable quality and performance.
Mar 2, 2018 at 2:28pm
I don't think these will become deprecated in C++ before they become deprecated in C.

std::random_shuffle() ("An implementation may use the rand function from the standard C library"), where C compatibility was not a concern, was deprecated (C++14) and removed (C++17).
Topic archived. No new replies allowed.