Thank you
Henry Wong wrote:Try the java.util.Random class.
Henry
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
I have no idea what you are talking about here. Maybe if you elaborate on your algorithm, we can discuss whether it matches your requirement or not?
Personally, I was just talking about generating a random number between 0 and 100, and then depending on where the number falls, do different tasks.
Henry
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.
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.
Exactly. Runs of the the same value are always possible. I found myself taking part in a little game with a coin a few weeks ago; I was "heads" every time . . . and won £10 as the last person standinguj nossnahoj wrote: . . . same number many times. Have you ever noticed an ordinary dice behave like that?
The government thinks you are too stupid to make your own lightbulb choices. But this tiny ad thinks you are smart:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
