Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mike619

macrumors newbie
Original poster
Jan 28, 2010
10
0
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.
 
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
 
Thanks.

Now how would i go about limiting the decimals places?

I want something like 2.14 and not 2.1465242
 
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;
}
 
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
 
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?
 
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?

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
 
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

Can you code this up for me? ;)
 
... 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.

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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.