CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: how do i generate a random float between 0 and 1??

  1. #1
    Join Date
    Jun 2005
    Posts
    94

    [FIXED] how do i generate a random float between 0 and 1??

    Hi, i need to generate a random float number between 0.0 and 1.0 for use in my OpenGL program but i can't seem to get it to work.

    i tried
    float r = (float) rand () / (GLfloat) RAND_MAX;
    but doesn't work coz my red colour ends up being 0.
    Last edited by thre3dee; August 4th, 2005 at 10:32 PM.

  2. #2
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: how do i generate a random float between 0 and 1??

    Code:
    #include <time.h>
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    int main()
    {
        srand( (unsigned)time( NULL ) );
    
        for (int i = 0; i < 5; i++) 
    	{
    		cout << (float) rand()/RAND_MAX << endl;
        }
    
        return 0;
    }

  3. #3
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: how do i generate a random float between 0 and 1??

    RAND_MAX is often 32767. If you want to make use of the full 23 bits of mantissa then:
    Code:
    const unsigned long RAND_MAX_PLUS_1 = RAND_MAX + 1;
    
    unsigned long bigRand()
    {
       return rand() * (RAND_MAX_PLUS_1) + rand();
    }
    the new RAND_MAX will be (RAND_MAX+1)-1
    Code:
    const unsigned long BIG_RAND_MAX = RAND_MAX_PLUS_1 * RAND_MAX_PLUS_1 - 1;
    float rand0to1()
    {
       return static_cast<float>(bigRand() ) / BIG_RAND_MAX;
    }
    (You can also make BIG_RAND_MAX a float or a double instead of an unsigned long).
    Note if you want to make use of the precision of double, you could require 4 calls to rand().

  4. #4
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,152

    Re: how do i generate a random float between 0 and 1??

    To get a better random number generator search for "Mersenne twister".
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  5. #5
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    Re: how do i generate a random float between 0 and 1??

    It's not good to rely on certain value of RAND_MAX constant. It's better to do smth like this:
    Code:
    float random()
    {
      float scale=RAND_MAX+1.;
      float base=rand()/scale;
      float fine=rand()/scale;
      return base+fine/scale;
    }
    "Programs must be written for people to read, and only incidentally for machines to execute."

  6. #6
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: how do i generate a random float between 0 and 1??

    Yes it is better to use RAND_MAX as a float or double because my unsigned long may well overflow.

    The compiler may be able to optimise your division by scale but I wouldn't count on it. If you make scale a const float it is more likely to.

    I don't know if you can assume that RAND_MAX will always be (2^N) - 1 for some N.

  7. #7
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,152

    Re: how do i generate a random float between 0 and 1??

    With all this playing with rand_max etc you are not sure if the resulting random numbers will be truly (pseudo-)random. Therefore, I suggest to use a Mersenne twister which can generate a floating point random number in 0-1.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  8. #8
    Join Date
    Jun 2003
    Location
    not-so-Great Britain
    Posts
    178

    Re: how do i generate a random float between 0 and 1??

    Hungarian notation is the bane of self documenting code.
    Forget all fancy tricks with operator precedence. Code should be easily readable.
    May the BOOST be with you.
    Good free E-Books thanks to Bruce Eckel.

  9. #9
    Join Date
    Jun 2005
    Posts
    94

    Re: how do i generate a random float between 0 and 1??

    no i fixed it i was setting it to a local float not the class member float so the class member waasn't actually getting the value.

  10. #10
    Join Date
    Nov 2002
    Location
    Foggy California
    Posts
    1,245

    Re: how do i generate a random float between 0 and 1??

    Read this FAQ.
    Kevin Hall

  11. #11
    Join Date
    Sep 2007
    Posts
    1

    Arrow My humble solution

    My humble solution


    float rand_FloatRange(float a, float b)
    {
    return ((b-a)*((float)rand()/RAND_MAX))+a;
    }

    Work fine with any range. All I did was to treat rand() as % and multiply that result by range between 'a' and 'b'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)