rean value generation of random number

I am facing problem to generate random number in real value. Let us consider l=0.045, so i need the random value between 0.0025 to 0.040. Can anybody help me how to write down programming code in c++ to determine real value generation of random number?
Last edited on
#include <stdlib>

write srand(time(NULL)); anywhere

then use rand()%100 to return random numbers between 0->100. Change 100 to whatever your desired range. Hope this helps
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
#include <iostream>
#include <random>
#include <ctime>

int main()
{
    // define a random number engine, seeded with a true random value
    // http://en.cppreference.com/w/cpp/numeric/random
    // http://en.cppreference.com/w/cpp/numeric/random/random_device
    std::default_random_engine rng( std::random_device{}() ) ;

    // if a a true random value is not available, seed it with a reasonably random value
    // std::default_random_engine rng( std::time(nullptr) ) ;

    // optional: warm up the engine
    rng.discard(1000) ;

    // define the required random number distribution. for instance:
    // http://en.cppreference.com/w/cpp/numeric/random/normal_distribution
    std::normal_distribution<double> normal_5_2( 50.0, 2.0 ) ; // mean 50.0, standard deviation 2.0
    // generate random numbers according to the distribution
    for( int i = 0 ; i < 10 ; ++i ) std::cout << normal_5_2(rng) << ' ' ;
    std::cout << '\n' ;

    // define the required random number distribution. for instance:
    std::uniform_real_distribution<double> uniform_10_99( 10.0, 99.0 ) ; // distributed in [ 10.0, 99.0 ]
    // generate random numbers according to the distribution
    for( int i = 0 ; i < 10 ; ++i ) std::cout << uniform_10_99(rng) << ' ' ;
    std::cout << '\n' ;
}

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