-4

How do I generate a random number in Java, (using Eclipse) that is weighted towards certain numbers?

Example, I have 100 numbers, each can be equally chosen 1% of the time. However, if a certain number, lets say 5, is displayed 8 times in that 100, it now has an 8% chance to be randomly selected, while the other numbers are still at 1%.

How can I write a random generator that gives me 10 numbers and gives the number 5 that 8% advantage to show up?

Thanks and sorry if I did not explain that well.

R.James
  • 13
  • 1
  • 2
  • Can you attempt to rephrase the question? Your second paragraph is basically just giving a simple probability problem. For your third paragraph, there is no way to give 10 numbers and an 8% probability that 5 is picked. Each number would have at least a 10% chance to be picked. :) – Dewick47 Jun 26 '17 at 15:23
  • 1
    Create an array with your 100 numbers and pick a random index. Seems simple. – OH GOD SPIDERS Jun 26 '17 at 15:23
  • 1
    I wouldn't agree with the second paragraph: If you pick number five out of 100 numbers eight times in a row, the next time that number five is displayed is still 1%, – Patrick Jun 26 '17 at 15:26
  • @Patrick - I think the OP is saying only one number is drawn. – Dewick47 Jun 26 '17 at 15:27
  • Create an array with 9 numbers except 5. Create a random number generator that generates 0 - 99 numbers. If the number falls from 0 to 7, show the number 5. If the generator show 8 to 99, create an random generator that generates 0 to 8 numbers and do `yourArray[yourRandomlyGeneratedNumber]`. – SedJ601 Jun 26 '17 at 15:31
  • 1
    @R.James: The way I understood the question the "100" numbers aren't necessarily 1-100 but could be "5, 5, 5, 5, 5, 9, 10, 11.... etc" and the bigger chance to select the number 5 simply comes from having the number multiple times in the list. Maybe the Asker can clarify and I understood it completly wrong. Are all of the 100 numbers unique? Are they in a sequence? – OH GOD SPIDERS Jun 26 '17 at 15:32
  • My apologies.. Correct.. There is an assortment of 100 numbers.. Not in order.. They can be 1,5,8,9,5,5,4,2,0,6, etc.. And the numbers can only be between 0-9.. I would like to do a random generator that gives the more 'populous' numbers a higher percentage to be randomly 'chosen' again. – R.James Jun 26 '17 at 15:39
  • @R.James: In that case my first guess was correct and you could simple put those numbers into an array or list and then select a random index. (See azros answer). – OH GOD SPIDERS Jun 26 '17 at 15:45
  • Awesome, I appreciate it.. I will give it a go.. I would like to apologize for not explaining the question as well as I could have.. I can see it in my mind what I need, writing it out - not so much!.. – R.James Jun 26 '17 at 15:48

1 Answers1

0
public static void main(String[] args){
    ArrayList<Integer> list = new ArrayList<Integer>();
    list.add(5);list.add(2);// to be continued ...        
    int randomInt = list.get((int) (Math.random() * list.size()));
    System.out.println(randomInt);
}

You'll get your List of values, and then you just need to pick randomly an index and get back the value

Math.random() will pick a value in [0;1[, if you multiply by the size of a List of 10 elements, you'll a value in [0;10[ , when you cast as an int you'll get 10 possibilities of index to look into the List

The ArrayList (implementation of List) allows duplicate element so : if you add 1,2,2,3,4,5,6,7,8,9 (the order does not matter) you'll have 10 elements with each one to be find with 1/10 = 10/100 = 10% of probability (aka chance), but the element 2 which appears twice will have 20% chance to be choosen in the method, more the number appears more it's chance to be choosen increase, with as you said (number of time the number appears)/(total number of element) (this is basic maths)

azro
  • 53,056
  • 7
  • 34
  • 70
  • can you explain how this will be weighted? – Michael Markidis Jun 26 '17 at 15:35
  • Also, you can use: https://docs.oracle.com/javase/8/docs/api/java/util/Random.html#nextInt-int- – Michael Markidis Jun 26 '17 at 15:38
  • @MichaelMarkidis it will be weighted according to the numbers you put in. e.G. if you just add 42 to the list as its only element 42 will be weighted with 100%. if you add 2 times 42 and 1 time 43 then you have a 2/3 chance of getting 42 or twice as likely as 43 and so on. – OH GOD SPIDERS Jun 26 '17 at 15:38
  • @OHGODSPIDERS That makes sense, but this answer does a poor job of illustrating that. – Michael Markidis Jun 26 '17 at 15:39
  • @MichaelMarkidis The probability depends on how often a certain number has been added to the list. You could have several `5s` in there to get the probability that you want. But OP did not specify if the numbers are from 1-100 and unique, so this answer might be correct or not. – hamena314 Jun 26 '17 at 15:40
  • @MichaelMarkidis i've updated the post – azro Jun 26 '17 at 20:35