generating a random float number

hi

I need to generate a random number between 0.0 and 1.0. But i'm having problems with it. I've tried a few options but each one gives me the same output which is not between 0.0 and 1.0.

I'm using a seed of 100 as well as the srand() function.

Here is my most recent attempt:
1
2
3
4
   double min = 0.0;
    double max = 1.0;
    random_number = min + rand() / (max + 0.1 - min);
    cout << random_number << endl;


Can anyone perhaps shed some light ?

Thank you
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <random>

double canonical_random_number()
{
    // https://en.cppreference.com/w/cpp/header/random
    // seed with a constant (constant seed sequence) to get repeatable results
    static std::mt19937 rng( std::random_device{}() ) ;  
    
    // https://en.cppreference.com/w/cpp/numeric/random/generate_canonical
    return std::generate_canonical< double, 128 >(rng) ;
}

int main()
{
    for( int i = 0 ; i < 20 ; ++i )
        std::cout << std::fixed << canonical_random_number() << ' ' ;
    std::cout << '\n' ;    
}

http://coliru.stacked-crooked.com/a/d373f6397618b2ba
Last edited on
I can only use c++ 98 standards.

So the above won't work for me but thank you.
I figured it out.

Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <random>
#include <chrono>
using namespace std;


// random_device() doesn't work on many systems


int main()
{
   unsigned int seed( chrono::system_clock::now().time_since_epoch().count() );

   const int N = 5;
   const double minimum = 0.0, maximum = 1.0;

   // Using rand()
   srand( seed );
   cout << N << " random numbers in [ " << minimum << ", " << maximum << " ] using rand():\n";
   for ( int i = 0; i < N; i++ ) cout << minimum + ( maximum - minimum ) * rand() / RAND_MAX << '\n';

   // Using functions in <random>
   mt19937 gen( seed );
   uniform_real_distribution<double> dist( minimum, maximum );
   cout << N << " random numbers in [ " << minimum << ", " << maximum << " ] using <random>:\n";
   for ( int i = 0; i < N; i++ ) cout << dist( gen ) << '\n';
}
Last edited on
C++98, using the same algorithm as above (std::generate_canonical), but with the legacy rng:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <cstdlib>
#include <ctime>

double canonical_random_number()
{
    double S = std::rand() ;
    const double R = RAND_MAX + 1.0 ;
    double M = 1.0 ;

    for( int i = 1 ; i < 16 ; ++i )
    {
        S += std::rand() * M ;
        M *= R ;
    }

    return S / M ;
}

int main()
{
    std::srand( std::time(0) ) ;
    double min = 1.9, max = 0.0 ;
    
    for( int i = 0 ; i < 25 ; ++i ) 
    {
        for( int i = 0 ; i < 40 ; ++i ) 
        {
            const double r = canonical_random_number() ;
            if( r > max ) max = r ;
            if( r < min ) min = r ;
            std::cout << std::fixed << r << ' ' ;
        }
        std::cout << '\n' ;
    }
    
    std::cout << "min: " << min << "   max: " << max << '\n' ;
}

http://coliru.stacked-crooked.com/a/32ef0379f671a1ff
Last edited on
Topic archived. No new replies allowed.