RAND_MAX Help

Well, the internet isn't much of a help, since it doesn't really give any indication how to, or if it's even possible, and in a few cases it just assigned a variable named randMax, but...

I actually just want to know how to set a maximum value for a pseudo-random number generator. I'm working through Stroustrup's book, and on chapter 10 we're exploring fstreams, and he has a struct for reading hours and temperatures. Well, I want something to simply give realistic values, and 32,000 (or more) degrees in any measuring standard is clearly unrealistic.

I know for what it's worth it doesn't matter, but it's still good rand practice, so I wanted to set it between 15-50 degrees for a fair distribution of the current month in Fahrenheit. Thus far all I have is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "../../../StroustrupSupportFiles/std_lib_facilities.h"

struct Reading
{
	int hour;
	int temperature;
};

int main()
{
	cout << "Enter output file: ";
	ofstream ofs{ "textout.txt" };
	srand(time(0));
	int hour;
	int temperature = (rand() % 14) + 1;
}


Where the include is just Stroustrup's preconfigured all-purpose header. Also, I'm aware it'll give randomized results whenever it's run, and that's OK, as long as they're within reason. I can write the program to get it into a vector (or just write directly) no problem, but bounding the random generation is where I'm stumped.
What does your line 15 does then, if you really are stumped?
http://www.cplusplus.com/reference/cstdlib/rand/


However, the rand is a dinosaur; best left alone and dead.

The new puppy, even though more verbose, is clear, cute and uniform:
http://www.cplusplus.com/reference/random/uniform_int_distribution/

The weather at least used to be more "normal":
http://www.cplusplus.com/reference/random/normal_distribution/
What does your line 15 does then, if you really are stumped?


Oh. Oh my.....

Thank you good sir! I see the extremely foolish mistake I've made. Clearly the notes I've taken were subpar and need revised. In fairness to me, I've only used rand() twice :/

I appreciate it! I'll look into uniform distribution later, but UNFORTUNATELY at this time I'm not QUITE advanced enough to delve into it, but I'm definitely going to note it (in better form than my notes for rand() obviously) because you're not the first person to note that rand() is a bit of a dinosaur function. I just like it because I'm a noob and it's (supposed to be) easy to use. I think we can all understand that!

Many thanks!
Topic archived. No new replies allowed.