Creating Histogram Data

Hi Guys,

I am struggling with creating data for a histogram.

I have made a vector that is of the form P =size_of_P(4), .4, .65, .8, 1

I am creating a random number and I want to count where it falls over and over again. the size of the histogram boxes are box1 [0 .4], box2[.4 .65] box3[.65 .8] box4[.8 1]. For each random number I get I want to place it or simply count it in the correct box. I am not exactly sure how to implement this.
Last edited on
I was thinking something along the lines of :

1
2
3
4
5
6
7
8
9
for(int t=0;t<no_of_trials;t++){
		float G = get_uniform_random_number();
		
		for(int i=1;i<P.size();i++){
			if(G < P[i]){
				count[i]++;
				G = get_uniform_random_number();
						}
		}
The real issue with this is that if G = .1 then it is less than every value of P so they all get a +1. I only want it to happen for the first time that G < P[i] not for every time it is. Any suggestions?
Now I'm trying this. This outputs: 5,6,7,0 for count[0],count[1],count[2],count[3] which doesn't make sense.


1
2
3
4
5
6
7
8
9
10
11
for(int t=0;t<no_of_trials;t++){
		float G = get_uniform_random_number();
		
		for(int i=0;i<P.size()-1;i++){
			if(P[i] < G < P[i+1]){
				count[i]++;
				G = get_uniform_random_number();
			}
		}
         }
Topic archived. No new replies allowed.