Thread: rand() for floats

  1. #1

    rand() for floats

    How do I do rand() for floats? Obviously this won't work:
    Code:
    float a = (float)rand() % 5.2;
    So how do I do it?

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    How about something like:
    Code:
    float R = (float)((rand()%100)/10)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    That kept giving me whole numbers for some reason. I don't get it, ten doesn't evenly divide into all numbers.

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    D'oh! Should've been:
    Code:
    float R =  float(rand()%1000) / 100 ;

    Or actually probably should've used a C++ cast...but anyways...

    Edit: I should mention that someone else probably knows a better way, but at least this works
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    Only way I can think of is to seed rand to a float. Try this:
    Code:
    	srand(.5);
    	float R = (float)((rand()%100)/.55);
    this seemed to work for me. If you only divide by a tenth, it gives an int...play around with it.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  6. #6
    thx dude!

    *gives a straight huggle

    *wait a minute, I'm a guy......saying huggle? Man, I got issues.

    edit: Okay! I got this worked out! You don't even need to seed with a float, just seed it with time as normal. Here is my function:
    Code:
    inline float FloatRand(float MaxVal)
    {
        return float(rand()%100)/((100/MaxVal)+0.1);
    }
    If you're wondering what the "+0.1" is for at the end, after some testing, I found this makes it more random. Before it would just randomize to 2 decimal places, this lets itself max out the decimal places.
    Last edited by frenchfry164; 11-24-2003 at 08:22 PM.

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    float FloatRand( float MaxVal )
    {
    
    	return ( (float)rand( ) / (float)RAND_MAX ) * MaxVal;
    
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by XSquared
    Code:
    float FloatRand( float MaxVal )
    {
    
    	return ( (float)rand( ) / (float)RAND_MAX ) * MaxVal;
    
    }
    This is indeed the best way.

    Do note that this can SOMETIMES return MaxVal; so the range of numbers is [0,MaxVal] -- it includes BOTH endpoints. If this is not what you want, you need to check for that condition.

    I mention this because often you want a number, for example, in the range [0,1) -- and a value of exactly 1 might break everything, once every RAND_MAX times you call the function. Very difficult to debug. In this case, you could use something like:

    Code:
    float r;
    while ( (r = FloatRand(1)) == 1);
    // now r is in the range [0,1).
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  9. #9
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    float FloatRand( float MaxVal )
    {
    
    	return ( (float)rand( ) / ( (float)RAND_MAX + 1.0f ) ) * MaxVal;
    
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed