- 145.6k
- 22
- 190
- 479
Speed and efficiency
Just don't worry about it. You need to generate 1000 random numbers, and do a little bit of accounting work. Any reasonable solution, such as yours, will perform similarly.
Types
Why use an array of char
to keep track of the number of occurrences? You're putting 1000 random numbers into 10 bins. What if one of those bins gets more than 127 items? You would get an overflow.
In summary, those bins should be of type int
. Don't even think about skimping on a few bytes of storage, if you want your code to be not-a-mess.
Naming
nums
actually stores counts. count
, as you've used it in the second loop, actually refers to the generated numbers. I suggest the following renaming:
nums
→bins
count
→i
Bias
The code will not fill the 10 bins with uniform probability.
Speed and efficiency
Just don't worry about it. You need to generate 1000 random numbers, and do a little bit of accounting work. Any reasonable solution, such as yours, will perform similarly.
Types
Why use an array of char
to keep track of the number of occurrences? You're putting 1000 random numbers into 10 bins. What if one of those bins gets more than 127 items? You would get an overflow.
In summary, those bins should be of type int
. Don't even think about skimping on a few bytes of storage, if you want your code to be not-a-mess.
Naming
nums
actually stores counts. count
, as you've used it in the second loop, actually refers to the generated numbers. I suggest the following renaming:
nums
→bins
count
→i
Bias
The code will not fill the 10 bins with uniform probability.
Speed and efficiency
Just don't worry about it. You need to generate 1000 random numbers, and do a little bit of accounting work. Any reasonable solution, such as yours, will perform similarly.
Types
Why use an array of char
to keep track of the number of occurrences? You're putting 1000 random numbers into 10 bins. What if one of those bins gets more than 127 items? You would get an overflow.
In summary, those bins should be of type int
. Don't even think about skimping on a few bytes of storage, if you want your code to be not-a-mess.
Naming
nums
actually stores counts. count
, as you've used it in the second loop, actually refers to the generated numbers. I suggest the following renaming:
nums
→bins
count
→i
Speed and efficiency
Just don't worry about it. You need to generate 1000 random numbers, and do a little bit of accounting work. Any reasonable solution, such as yours, will perform similarly.
Types
Why use an array of char
to keep track of the number of occurrences? You're putting 1000 random numbers into 10 bins. What if one of those bins gets more than 127 items? You would get an overflow.
In summary, those bins should be of type int
. Don't even think about skimping on a few bytes of storage, if you want your code to be not-a-mess.
Naming
nums
actually stores counts. count
, as you've used it in the second loop, actually refers to the generated numbers. I suggest the following renaming:
nums
→bins
count
→i
Bias
The code will not fill the 10 bins with uniform probability.