Getting a float random number in a given range

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alishaikhji
    New Member
    • Jun 2009
    • 1

    #1

    Getting a float random number in a given range

    I am working on a program which will need several different integer and float random numbers at different stages, for example:
    - At one point, I need a random number (float) in the range 0.1 to 10.0
    - At one point, I need a random number (float) in the range 0.5 to 1.5
    - At one point, I need a random number (float) in the range 0.3 to 3.0
    - At one point, I need a random number (float) in the range 0.1 to 10.0

    Also, I need to make it sure that it generates different random numbers everytime each of these functions is called in the program.

    For the integer random numbers, rand () is generating the same random number everytime, so I tool help from planet source code's algorithm which is as follows:
    =============== =============== ======
    #include <iostream.h>
    #include <time.h>
    void init_mm( );
    int number_range( int from, int to );
    int number_mm( void );
    static int rgiState[2+55]; // leave this alone
    int main()


    {
    init_mm(); //seed the number generator
    int random = number_range( 0, 1 );
    cout << random << endl;
    }
    int number_mm( void )


    {
    int *piState;
    int iState1;
    int iState2;
    int iRand;
    piState = &rgiState[2];
    iState1 = piState[-2];
    iState2 = piState[-1];
    iRand = ( piState[iState1] + piState[iState2] )
    & ( ( 1 << 30 ) - 1 );
    piState[iState1] = iRand;
    if ( ++iState1 == 55 )
    iState1 = 0;
    if ( ++iState2 == 55 )
    iState2 = 0;
    piState[-2] = iState1;
    piState[-1] = iState2;
    return iRand >> 6;
    }
    /*
    * Generate a random number.
    */
    int number_range( int from, int to )


    {
    int power;
    int number;
    if ( ( to = to - from + 1 ) <= 1 )
    return from;
    for ( power = 2; power < to; power <<= 1 )
    ;
    while ( ( number = number_mm( ) & ( power - 1 ) ) >= to )
    ;
    return from + number;
    }
    /*
    * This is the Mitchell-Moore algorithm from Knuth Volume II.
    */
    void init_mm( )


    {
    int *piState;
    int iState;
    piState = &rgiState[2];
    piState[-2] = 55 - 55;
    piState[-1] = 55 - 24;
    piState[0] = ( (int) time( NULL ) ) & ( ( 1 << 30 ) - 1 );
    piState[1] = 1;
    for ( iState = 2; iState < 55; iState++ )


    {
    piState[iState] = ( piState[iState-1] + piState[iState-2] )
    & ( ( 1 << 30 ) - 1 );
    }
    return;
    }

    =============== ==========
    when i optimized the code to generate floating random numbers from 0.1 to 10.0 range, it was generating like 1.1, 2.1, 6.1 etc which is not purely random.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Hi,
    There rf unctions like drand(),drand48 () etc...
    Better go to these man pages and try to use those.
    But i am doubtful that you can get a random number in that range.

    Raghu

    Comment

    • RRick
      Recognized Expert Contributor
      • Feb 2007
      • 463

      #3
      Also, pick a random number generator that takes a seed value. By adjusting the seed value, you change each string of random numbers.

      The seed value can be simply a counter that you increment. You can also use the low values from gettimeofday.

      As for getting the correct range, find out what the total range is for the generator. The random number divided by the range will give you a value from 0.0 to 1.0. From here you can adjust the value to suit your own specific intervals.

      Comment

      Working...