Generating random float numbers(C++)

Discussion in 'Mac Programming' started by mike619, Feb 8, 2010.

  1. mike619 macrumors newbie

    Joined:
    Jan 28, 2010
    #1
    I have an assignment where i am to generate a specified number of random student records. One of the elements i'm to generate is GPA.

    I cant seem to figure out how to generate random float numbers between 0.00 and 4.00 using the rand() function. For example: 3.21

    Any help is appreciated, thanks.
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    Code:
    #include <stdio>
    #include <stdlib.h>//I think .h? I don't use C++ much
    
    int main(int argc, char *argv[]) {
     double rGPA = 0.;
     rGPA = ((double)rand()/(double)RAND_MAX)*4.;
     std::cout << "The random GPA is: " << rGPA << std::endl;
    }
    
    So basically, what you've got is:
    a random number between 0 and RAND_MAX, divided by RAND_MAX. This should yield a floating point value between 0 and 1. You then quadruple that thing, and get a random number between 0 and 4. This probably isn't that rigorous, but rand() is only pseudorandom to begin with, so it's probably good enough for government work. I honestly don't have any suggestions on adding to the randomosity other than pointing a webcam at a lava lamp and summing/xor'ing/etc. its CMYK and RGB (plus intensity) using 8-bit values for each to get 64-bits per pixel, then treating the resulting 64-bit value as a double. You could just make an array that is sizeof(double)/sizeof(int) long, filling in each element with the result of rand(), then memcpy'ing those total bytes over to a double. You'd then need to do some sort of double modulo 4.(see here: http://bytes.com/topic/c/answers/495889-modulus-double-variables), but that's probably a little weird, though it should work, too.

    -Lee
     
  3. mike619 thread starter macrumors newbie

    Joined:
    Jan 28, 2010
    #3
    Thanks.

    Now how would i go about limiting the decimals places?

    I want something like 2.14 and not 2.1465242
     
  4. Littleodie914 macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #4
    Google

    First link. :D
     
  5. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #5
    lee1210 you're quick I was just about to post pretty much the same thing, checked to see if anyone responded and of course there you were.

    Code:
    #include <climits>
    #include <iostream>
    
    #include <ctime>
    
    int main(int argc, char* const argv[])
    {
        srand(time(NULL));
        for ( int i = 0; i < 100; i++ )
        {
            std::cout << "Value: " << (4.0 * (rand() / (double)RAND_MAX)) << std::endl;
        }
    
        return 0;
    }
    
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    Now that the OP has clarified, I would like to stress that this isn't a floating point issue at all, so you should use shifted fixed point math for this. Use a short or int, and use rand() to get a value between 0 and RAND_MAX. Then use mod 401 to get a value between 0 and 400. Yes, this means your lower bits are all that get used. Oh well. Again, this isn't rigorous, but rand() in modern libraries should give the same randomisity across all bits, though that hasn't always been the case. Do integer math for everything until you need to display, then display as:
    Code:
    std::cout << value / 100 << "." << value % 100 << std::endl;
    This way all of your math is exact, and you don't have to deal with poor estimation of floating point math.

    -Lee
     
  7. mike619 thread starter macrumors newbie

    Joined:
    Jan 28, 2010
    #7
    Still a bit new to C++, but what is the difference between srand() and rand()?

    I noticed that as i ran my code multiple times - though the numbers do seem random, i get the same sequence each time i run it. Any way to prevent/change that?
     
  8. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #8
    That's so you can debug. Once you're ready, you have to seed the pseudorandom number generator with srand. People often use time(NULL) to pass in the current time as the seed.

    -Lee
     
  9. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #9
    I really like this online reference of both the "C" and "C++" library.

    Reference for 'srand'.
     
  10. Sydde macrumors 68020

    Sydde

    Joined:
    Aug 17, 2009
    #10
    Can you code this up for me? ;)
     
  11. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #11
    Gotta be careful with that. NaNs, zeros, infinities, and denormalized values will play havoc if they show up in the bit patterns.

    Personally, I'd just read bytes from /dev/random, which on Mac OS X uses the Yarrow algorithm.
    http://en.wikipedia.org/wiki/Yarrow_algorithm
     

Share This Page