John R
John R

Reputation: 3036

Java: do something x percent of the time

I need a few lines of Java code that run a command x percent of the time at random.

psuedocode:

boolean x = true 10% of cases.

if(x){
  System.out.println("you got lucky");
}

Upvotes: 25

Views: 45370

Answers (8)

Crystark
Crystark

Reputation: 4236

To take your code as a base, you could simply do it like that:

if(Math.random() < 0.1){
  System.out.println("you got lucky");
}

FYI Math.random() uses a static instance of Random

Upvotes: 4

You could try this:


public class MakeItXPercentOfTimes{

    public boolean returnBoolean(int x){
        if((int)(Math.random()*101) <= x){ 
            return true; //Returns true x percent of times.
        }
    }

    public static void main(String[]pps){
        boolean x = returnBoolean(10); //Ten percent of times returns true.
        if(x){
            System.out.println("You got lucky");
        }
    }
}

Upvotes: 1

reznic
reznic

Reputation: 710

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

Upvotes: 1

Scott Stanchfield
Scott Stanchfield

Reputation: 30652

You can use Random. You may want to seed it, but the default is often sufficient.

Random random = new Random();
int nextInt = random.nextInt(10);
if (nextInt == 0) {
    // happens 10% of the time...
}

Upvotes: 2

WhiteFang34
WhiteFang34

Reputation: 72059

You just need something like this:

Random rand = new Random();

if (rand.nextInt(10) == 0) {
    System.out.println("you got lucky");
}

Here's a full example that measures it:

import java.util.Random;

public class Rand10 {
    public static void main(String[] args) {
        Random rand = new Random();
        int lucky = 0;
        for (int i = 0; i < 1000000; i++) {
            if (rand.nextInt(10) == 0) {
                lucky++;
            }
        }
        System.out.println(lucky); // you'll get a number close to 100000
    }
}

If you want something like 34% you could use rand.nextInt(100) < 34.

Upvotes: 35

Jack
Jack

Reputation: 133609

If by time you mean times that the code is being executed, so that you want something, inside a code block, that is executed 10% of the times the whole block is executed you can just do something like:

Random r = new Random();

...
void yourFunction()
{
  float chance = r.nextFloat();

  if (chance <= 0.10f)
    doSomethingLucky();
}

Of course 0.10f stands for 10% but you can adjust it. Like every PRNG algorithm this works by average usage. You won't get near to 10% unless yourFunction() is called a reasonable amount of times.

Upvotes: 30

Jesus Ramos
Jesus Ramos

Reputation: 23266

You could always generate a random number (by default it is between 0 and 1 I believe) and check if it is <= .1, again this is not uniformly random....

Upvotes: 0

ptikobj
ptikobj

Reputation: 2710

You have to define "time" first, since 10% is a relative measure...

E.g. x is true every 5 seconds.

Or you could use a random number generator that samples uniformly from 1 to 10 and always do something if he samples a "1".

Upvotes: 0

Related Questions