Thread: Random Decimals

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    15

    Random Decimals

    Hi

    Ive found lots tutorials detailing how to create random integer values, but I was wondering how to create random decimal values between a range?

    Thanks for any help

    TJJ

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    do something like this:
    Code:
    float number=(1+rand()%10000)/100.0;
    the only thing random about that code is the rand() part... take notice that in the formula 1+rand()%100;, the 1+ just adds one to whatever comes out, and the %100 is just performing a modulo on whatever rand() comes out to be...

    don't forget to put this somewhere near the beginning of your program:
    Code:
    srand(time(0));
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >float number=(1+rand()%10000)/100.0;
    float number=(float)(1+rand()%10000)/100.0;
    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

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    No you don't need that float casting there because the number he's dividing (100.0) is already a float. Well, technically its a double so you'd have to make it 100.0F.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    (1+rand()%10000) isn't a floating point number. Usually, when performing operations on mixed types, the result is an integer value; so yes, the typecast is necessary if only to eliminate ambiguity.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    No: only one of the operands has to be a floating point value for the result to be floating point. The (1+rand()%10000) part yields an integer but the 100.0 is a floating point value. So when you divide, the result will be a floating point value.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Or heck why not use the better approach of :
    Code:
    float floatrandom(float lowerbound, float upperbound)
    {
      return ((float)rand() / RAND_MAX) * (upperbound - lowerbound) + lowerbound;
    }
    The mod operator shouldn't be used for getting random numbers. For example if you wanted to get a number between 0 and 65535 but your RAND_MAX was only 32767 then using the mod operator will make us SOL. However by using the rand()/RAND_MAX method you have now extended your range to handle any number you want.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>No: only one of the operands has to be a floating point value for the result to be floating point.
    Since when? I'm pretty sure I recall encountering a bug caused by this, where I had one integer and one floating-point (I think I was trying to convert degrees to radians or vice versa), and the whole thing was converted to an integer and rounded down, causing some very funked out results.

    I'll run a test though, maybe you're right.

    **EDIT**
    Grr, you're right Learn something new every day...
    Last edited by Hunter2; 04-26-2004 at 03:49 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. How to correctly implement random decimals?
    By ZooTrigger1191 in forum C++ Programming
    Replies: 5
    Last Post: 02-17-2003, 09:57 AM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM