How to create a random number in a certain percentage of time

I am trying to create a random number that fills a certain percentage of the time. For example, I want to create an array that is set between a certain range (say, 0-100), but in 10% of the cases I want a random number selected to be multiplied by n. How can I create this "random" number n% of the time?

edit I have not tried this in Java yet, I was only trying to write out the algorithm manually, but I just got stuck. What I will have is a variable that contains a number, let's say that the number is 3000, and a random number from the array will be subtracted from the number 3000 until it reaches 0, but a certain percentage of the time I want the number output from the array multiplied by a certain percentage of the time.

+4
source share
2 answers

You can do something 10% of the time by selecting a random number from 0 to 100 and triggering an event if the number is less than 10. So, select your random number. Then choose another random number from 0 to 100, and if this second number is less than 10, you multiply the first by n.

+2
source
public static boolean getRandPercent(int percent) { Random rand = new Random(); return rand.nextInt(100) <= percent; } 
+1
source

Source: https://habr.com/ru/post/1387603/


All Articles