You guys are my only hope. (generating random numbers)

Discussion in 'Mac Programming' started by unkn0wnsoldier, Apr 7, 2010.

  1. unkn0wnsoldier macrumors regular

    unkn0wnsoldier

    Joined:
    Jul 4, 2009
    Location:
    Fountain, Co
    #1
    Hello all!

    I've been working on an assignment for days and I just can't figure it out. I really wanted to figure it all out on my own but I am stumped. What I have to do is: generate 5000 random numbers between 1-53, then see how many times each number comes up. Supposed to look something like this:

    number count
    1 235
    2 500
    3 5
    ... ...
    53
    The most frequent generated number was:
    The least frequent generated number was:

    I created a for loop that runs to 5000 to populate my random_number array.
    I created another array and loop to hold the number of times each number was generated.
    What I can't figure out is how to read through the entire random_number array and transfer the count to my other array. I'm really lost. Any help would be much appreciated. My code is just about non-existant because I know it is something simple to link these two things together.

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <ctime>
    using namespace std;
    	
    
    
    
    int main()
    
    {
    	int random[5000] = {0};	//random number array
    	int number[53] = {0};
    	int x, y;
    	
    	
    		srand(time(0));		//random seed
    	
    	for (x = 0; x < 5000; x++) {
    		lucky_number = 1 + rand() % 53;
    		random[x] = lucky_number;
    		}
    	
    for (y = 0; y < 53; y++) {
    	cout << y+1 << " " << number[y] << endl;
    }
    }
    Thanks,
    Pat
     
  2. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #2
    Hints:

    If I understand the requirements correctly you don't need the array 'random[5000]'.

    And you'll need to preform three loops.

    One to increment a value in for each time a number between 1 and 53 inclusive occurs.

    Another to display the number of times each of the numbers 1 thu 53 inclusive has been generated.

    And another to track the random number generated both the least and most number of times.

    Edit: Or you could combine loops 2 and 3.
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    Why do you have to generate the random numbers in an array?

    Count them as they are generated. You don't need all the random numbers at the same time.

    Think about how you'd do this using paper and pencil, and a spinner that randomly returns 1-53.
    http://en.wikipedia.org/wiki/Spinner_(game)

    Paper and pencil:
    Code:
    make a grid of boxes, labeled 1-53.
    repeat 5000 times
      spin the spinner, read the resulting number
      add a tally mark to the number's box on the grid
    after 5000 repetitions, count the tallies in each box
    note the box with fewest and highest counts.
    
    http://en.wikipedia.org/wiki/Tally_marks

    Map the pencil-and-paper approach into pseudo-code.

    Pseudo-code:
    Code:
    make array of counters indexed by 1-53
    repeat 5000 times
      generate a random number in range 1-53
      count it in the array
    print results from array (find min and max counts while printing)
    
     
  4. unkn0wnsoldier thread starter macrumors regular

    unkn0wnsoldier

    Joined:
    Jul 4, 2009
    Location:
    Fountain, Co
    #4
    If I'm understanding correctly, I need to lose the random_number[5000] and use my number[] to tally the number as it is generated? The only way I can think of how to do this is by using if else statements. Like:
    if(lucky_number == 1)
    number[0] = counter++;
    if(lucky_number == 2)
    number[1] = counter++;

    I know that isn't the way to go about it, but that is the only solution that is coming to mind.
     
  5. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #5
    Yes.

    You don't need a bunch of IF statements. A single assignment statement will do. You just have to use an expression as the subscript to add one to the correct element of the number[] array.

    Pseudo-code:
    Code:
    add one to number[some expression]
     
  6. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #6
    You're getting closer.

    Another clue: array[array_index]++ increments the value in array[array_index].
     
  7. unkn0wnsoldier thread starter macrumors regular

    unkn0wnsoldier

    Joined:
    Jul 4, 2009
    Location:
    Fountain, Co
    #7
    Pseudo-code:
    Code:
    add one to number[some expression]
    __________________

    This is exactly what was holding me up the entire time. I couldn't figure out how to increment the array element. As you guys posted, I was just typing:

    for (x = 0; x < 5000; x++) {
    lucky_number = 1 + rand() % 53;
    number[lucky_number - 1] = number[lucky_number] + 1;

    but I used number[lucky_number-1]++.

    Is that the correct way to do it? I did [lucky_number-1] because the array starts at 0? Am I on the right track now?

    Btw, I don't know how to quote more than one post at a time.
     
  8. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #8
    Yes that is correct.

    I've never used it but multi-quote is done with the icon to the right of the quote icon.

    I'd like to see you post your completed assinment.
     
  9. unkn0wnsoldier thread starter macrumors regular

    unkn0wnsoldier

    Joined:
    Jul 4, 2009
    Location:
    Fountain, Co
    #9
    Thank you!

    Here is what I'm turning in this morning. I was able to get everything to work except for the number that came in the least. I stayed up until 2 am reading the book and trying to figure it out. I really want to thank everyone who has offered hints and assistance. If it wasn't for you all, I would be turning in nothing instead of my program with one error. Thank you.

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <ctime>
    #include <iomanip>
    using namespace std;
    	
    
    
    
    
    int main()
    
    {
    	int lucky_number;
    	int counter = 0;	
    	int number[53] = {0};	
    	int x, y;
    	int index;
    	int most = 0;
    	int least = 0;
    	
    	
    	
    	cout << "Pat's Random Number Generation" << endl;
    	cout << "************************************";
    	
    	cout << setiosflags(ios::right) << setw(5) << "\n\n Number  # of times\n\n";
    		srand(time(0));		//random seed
    	
    	for (x = 0; x < 5000; x++) {
    		lucky_number = 1 + rand() % 53;
    		number[lucky_number - 1]++;
    		
    		
    	}
    	
    for (y = 0; y < 53; y++) {
    		if (number[most] < number[y]) {
    			most = y;
    			
    		}
    	cout << setiosflags(ios::right) << setw(3) << y+1 << " " << setw(10)<< number[y] << endl;
    }
    	for (index = 0; index < 53; index++) {
    		if (number[most] < number[index]) {
    			most = number[index];
    		}
    		
    		}
    		
    for (counter = 0; counter < 53; counter++) {
    	if(number[least] > number[counter]) {
    		least = number[counter];
    }
    			
    }
    		
    	
    	cout << "\nThe number that came in the least was " << least  << endl;
    
    	cout << "\nThe number that came in the most was " << most + 1 << endl;
    }
    
    
    
     
  10. jpyc7 macrumors 6502

    Joined:
    Mar 8, 2009
    Location:
    Denver, CO
    #10
    It's probably too late to help. I think your "most" and "least" variables should be used only as the values of the largest and smallest numbers. You're also using them as indexes into an array. I think as a result, the "most" value works despite the fact that it should probably fail sometimes. And by happenstance, your "least" always seems to fail but it might work sometimes.

    I've just looked over your program without trying to run it, so I might not be right.
     
  11. unkn0wnsoldier thread starter macrumors regular

    unkn0wnsoldier

    Joined:
    Jul 4, 2009
    Location:
    Fountain, Co
    #11
    You're spot on. The most has worked every time I ran it and least never works. I really wanted to sort it out but time just ran out. Next time, I'll ask for help well in advance rather than wait until the last minute. If I can get a seat in class early, I'm going to have another look at it. What stinks is that I was not procrastinating on this. The assignment was handed out two weeks ago and I started on it as soon as I got home. After not getting anywhere, I took a break, then looked at it again a day or two later, did the same a few days after that. Then I called my classmates. None of them even started the assignment until last night. They were having the same problems that I was. It's going to be interesting to see who doesn't show up because they didn't do the homework or had problems.

    Thanks.

    Edit: the way that I got my most value was straight out of my textbook, to the letter.
     
  12. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #12
    By the way, I know you didn't end up using the code above, but notice the bug in the subscript indexing. That's one reason why using the "++" operator is useful: it's easier to read and reduces the chances of bugs like this.
     
  13. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #13

    Maybe you'll get extra credit for learning how to use the Multi-Quote forum feature! :)


    To reply to more than one post with quotes, click the [​IMG] icon next to each post that you'd like to reply to. The icon will change color. You can click again to deselect any of them. When you've selected the set of posts, click the [​IMG] button and all of the posts will be quoted in the message text for you. You can type your replies at the end or intersperse them with the quoted messages, as I've done here.
     
  14. unkn0wnsoldier thread starter macrumors regular

    unkn0wnsoldier

    Joined:
    Jul 4, 2009
    Location:
    Fountain, Co
    #14
    I'll bring that up at Tuesday's lecture:D
     
  15. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #15
    You have two separate loops that potentially affect 'most'.

    The first loop, where y iterates over the array, is correct. It finds the index of the highest count in the array. This index represents the most frequently generated number.

    The second loop, where index iterates over the array, is wrong. Fortunately, the second loop will never execute the body of its 'if' block, because 'most' is already holding the index of the highest count in the array. So even though it's wrong, it causes no damage.

    Then your loop that determines a value for 'least' follows the pattern of your second 'most' loop, where the body of the 'if' block assigns a value to least. The value being assigned to least is wrong.

    Walk through your least loop with pencil and paper. Write down 5 numbers in 5 boxes, representing an array of only 5 counts instead of 53. Assume the counts in the array are all above 10. Use the same starting and looping conditions: least = 0, counter = 0, and count++ at each iteration. See what happens.
    Code:
    +---+---+---+---+---+
    | 27| 33| 54| 21| 17|
    +---+---+---+---+---+
    
     
  16. jctj macrumors newbie

    Joined:
    Feb 24, 2010
    #16
    3 different for loops

    You have 3 different loops for sorting through your array.

    The first one uses "y" as the counter and works great. Just as it should.

    The second one uses "index" as the counter and works only because the first one already worked flawlessly. That is, in the "y" loop you have already determined the correct value for "most" and so the second loop never changes the value of "most" - the second loop will not find a newer value that is greater, so it will never change the (correct) value of 'most'. The second loop is wrong though - it makes the same mistake that you make in the third loop.

    The third loop uses "counter" and the counter and is looking for 'least'. It does not work. That is because 'least' is supposed to be the number between 1 and 53 (i.e. the index in your array) but you then set the variable 'least' to equal the NUMBER OF TIMES THAT NUMBER IS HIT - not the number (or index) itself. In other words, if the number "21" is picked 80 times (and it is the least frequently picked number) then your "least" variable is set to "80" and NOT the "21" that you actually want.

    In short - drop the "index" and "counter" loops completely. Keep the "y" loop. You can, for efficiency, use the same "y" loop to find both the 'most' and 'least' indexes at the same time. Where you have
    Code:
    if(number[most] > number[y]) { most = y; }
    you can simply add another line doing the opposite thing for 'least'. Inside the "y" for-loop, add
    Code:
    if(number[least] < number[y]) { least = y;}
    Late at night is the WORST time to program...lol. Your brain will simply miss things it won't miss when you are awake and alert. Also - what Chown33 and others suggested is almost a necessity at some point. Play the computer with a piece of paper and a pencil, and run through your program line by line, doing EXACTLY what the code tells you to do. If you have an array - write the entire array down on paper ("number[1] = .... number[2] = ....") and then be painfully simple if following your own code EXACTLY - i.e. like a computer. This way you would have seen that you were assigning to 'least' the number of hits, not the number getting hit.
     

Share This Page