# Random insert and delete into an arraylist

S Gregg

Greenhorn

Posts: 15

posted 6 years ago

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.

Thank you

Thank you

S Gregg

Greenhorn

Posts: 15

posted 6 years ago

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

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

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

posted 6 years ago

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

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

Embla Tingeling

Ranch Hand

Posts: 237

posted 6 years ago

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 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

Greenhorn

Posts: 15

Embla Tingeling

Ranch Hand

Posts: 237

posted 6 years ago

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.

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.

Embla Tingeling

Ranch Hand

Posts: 237

Campbell Ritchie

Sheriff

Posts: 50278

80

posted 6 years ago

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?