Random Number Generation

Okay, so lets assume im using a simple number generator such as :

1
2
3
4
5
6
7
int ranNum;

srand (time(0));
ranNum = rand()% 10 + 1;

cout << ranNum;


If i were to add a loop that runs the program 100 times, how would i be able to record the likely hood of each number appearing?

Any help is appreciated!
Last edited on
Someone is going to show up and tell you not to use rand(). In all likelihood you probably shouldn't, but... it IS pretty easy to use...

ANYHOO...

If you just need to know how many times a number shows up (I assume based on a percentage) you could just increment a prebuilt vector of 10 ints (11 if you want them in their respective vector subscript) then do some simple math.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;

int main()
{
	srand(time(0));
	vector<int>occ(11, 0); //Initialize a vector of 11 ints to 0.
	for (size_t i = 0; i != 100; ++i) ++occ[(rand() % 10 + 1)]; //Straight to vector
	for (size_t i = 1; i != 11; ++i) { //We don't need occ[0]
		cout << i << " occurs "
			<< static_cast<double>(100.0 / occ[i]) << "% of the time.\n";
	}
	return 0;
}


I guess that could be one (admittedly lazy) method.

EDIT: I fail at casting, and this gives a stupid amount of decimal points. You can format that later if you choose.

EDIT EDIT: Fail at math too because that damned sure doesn't add to 100%...
Last edited on
Thanks for the quick reply, though admittedly i haven't began to use vectors. I understand everything you said except the occ part, what does that mean??
Last edited on
That's just the name of the vector, and you should definitely ignore (most) of the code I gave you, because I'm an idiot and it's wrong.

The idea behind it is (for the most part) fine, but there's definitely a logic error in there that's messing up the math.

EDIT: If you JUST want to use loops? Eh... I think you're going to have some rather large and ugly code. There are probably some fabulous ways the good people here know of that can do it, but if we're assuming you're ONLY using loops? Best I PERSONALLY could think of is you need 10 variables, a switch statement, and just start incrementing them one at a time, and that's just a bad idea. It would work, but more trouble than it's worth.
Last edited on
OHH, I see. I dont know how i didnt recognize that! One last question why did you use static_cast<double> in this? It dosent seem necessary to me. Everything else is very clear though! Much appreciated!
Last edited on
I didn't think it was necessary either, but then I thought, "Well maybe, perhaps, for some reason he would like to ignore significant digits and get a decimal approximation" and then the code completely failed anyway, for reasons I'm still trying to figure out, and will likely be very angry at whatever stupid mistake I've so clearly made...

EDIT: And you should definitely shun me publicly because the stupid mistake I made was a mathematical mistake only a true dummy would make, as there's absolutely no reason to be dividing by anything, or casting anything, or really DOING anything outside of outputting the number... no amount of excuses makes that acceptable...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <vector>
#include <ctime>
using namespace std;

int main()
{
	srand(time(0));
	vector<int>occ(11, 0);
	for (size_t i = 0; i != 100; ++i) ++occ[(rand() % 10 + 1)];
	for (size_t i = 1; i != 11; ++i) { //We don't need occ[0]
		cout << i << " occurs " << occ[i] << "% of the time.\n";
	}
	return 0;
}


I'm going to go weep quietly now...
Last edited on
> Static casts are prefered over C-style casts when they are available because they are both more restrictive (and hence safer) and more noticeable.

Accordingly, static cast is pretty much the same as normal typecast. But it is often more preferred to normal typecast simply it is more C++-like and it provides your code with more readablity.

Anyway, it is not needed. Not now...
Shame @Yawzheek! LOL

Thanks for all of the help everyone ! /thread
Anyway, it is not needed. Not now...


It never was. I couldn't have possibly screwed that math up more if I tried, and I'm thoroughly embarrassed. I don't even know how I did it. Used to be quite good with mathematics...

But as closed account says, the reason (one of the reasons - the only reason at this point that really matters) is because it's a MASSIVE warning sign to whomever reads the code that I'm intentionally doing something that there's a chance I shouldn't be doing, and if nothing else, it's a great learning experience for you, because:

A) YOU SAW IT! You saw it clearly, and that's part of the point. The "old" style of doing it is:
 
double(100.0 / occ[i]);


Which is rather inconspicuous in large code. It's quite easy to miss. There are likely several variations, but the thing to remember is you're doing something that is questionable (which you questioned, and rightly so) but with the giant and ugly:

 
static_cast<double>(100 / occ[i]);


It's REALLY noticeable. Hard to miss, and you called me out on my nonsense, which in turn helped me to notice a serious flaw in the code itself. Well, I noticed it before, but you also brought up an excellent point which helped me see it easier. But also:

B) Sometimes you might need to use it if there are circumstances out of your control. This was MOST DEFINITELY NOT ONE OF THOSE TIMES. Not at all. Not even a little bit. If the math had been slightly more complicated, perhaps, but finding the instances of a number from 100 is CLEARLY not one of those times, and again, I'm embarrassed for even suggesting it, but I'll leave it because... well, I made my bed, so climb on in...
Topic archived. No new replies allowed.