I have an assignment where I need to randomly insert or delete 70 objects in an array list. I know how to do this part but the assignment goes on to specify that 10% of the time, insert and integer,, 24% of the time, 6% of the time insert a string... This is where I am stuck and need some assistance, how do i get the program to randomly choose what to do in order to equal these percentages. I have only been doing java for 2 months and im pretty sure that maybe there is something im overlooking. Can someone point me in the right direction please.
I understand an have used the java.util.Random class before to generate random numbers which is what I will use to do part (a), what I don't understand is how to use it to achieve the following
60 % of the time insert an object
(a) 10 % insert an Integer between 12 and 134
(b) 24 % insert a RationalNumber with num 0 to 8 and den 1 to 23
(c) 20 % insert a point with both coordinates between -4.6 and 37.5
(d) 6% insert another copy of the string “hello#” the number increasing each time)
40% of the time delete an object
i know i can calculate 10% of 70 = 7 and insert 7 integers, then calculate 24% of 70 = 17 and insert 17 RationalNumbers, etc but this wouldn't be random
S Gregg wrote:i know i can calculate 10% of 70 = 7 and insert 7 integers, then calculate 24% of 70 = 17 and insert 17 RationalNumbers, etc but this wouldn't be random
Because you're dealing with whole percentage points you can think of a dice with 100 sides numbered from 0 thru 99.
You divide the numbers into groups. The size of each group will determine the probability of that group being selected. For example a group with the numbers 0 thru 9 has a probability 10%. A group with numbers 10 thru 33 will have a probability of 24%. A group with numbers 34 through 53 will have a probability of 20%. Etcetera.
So although each number on the dice has an equal probability of showing up, the group it belongs to has a probability of being selected which is proportional to its size. In principle it doesn't matter which numbers belongs to which group but using intervals is convenient programatically.
There's a method called nextInt of class Random you can use to simulate the dice.
Joined: Oct 13, 2009
I did gather that from what Henry Wong posted earlier and I tried to implement it but one issue I have is, what if several of the numbers generated are in the same group, that will throw the percentages off. Here is what I have so far
Joined: Oct 22, 2009
S Gregg wrote:I did gather that from what Henry Wong posted earlier and I tried to implement it but one issue I have is, what if several of the numbers generated are in the same group, that will throw the percentages off.
No it doesn't. Say you have an ordinary dice with 6 sides instead and you make two groups. One with 1, 2 and 3. and the other with 4, 5 and 6. Both groups will have a 50% chance of being selected and that's regardless of how many times the dice is thrown. In fact throwing the dice and assigning the outcome to the two groups is equivalent to flipping a coin. The probability of getting heads or tails doesn't change regardless of how many time the coin is flipped. What will change is the number of heads and tails in a series of say 70 flips. The longer the series the closer to 50/50 you will come.
Joined: Oct 22, 2009
S Gregg wrote: Here is what I have so far
Note that you don't have to check the lower bounds. This,
is equivalent to this,
Why are you preventing the 100 sided dice from coming up with the same number many times. Have you ever noticed an ordinary dice behave like that?