Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Random 0/1 with given possibility

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Random 0/1 with given probability

    I'm trying to get a random value of 0 or 1, with given probability (like 0,7) of it being 1.

    Which function can I use in such a case?
    Where can I find that function better explained?

    Any help would be very apriciated.
    Last edited by juwan; November 10th, 2010 at 11:08 AM. Reason: Wrong use of a word


  2. #2

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Random 0/1 with given possibility

    I look over util.random and math.random, but I was't able to find anything, that would allow me to set the probability.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Random 0/1 with given possibility

    Quote Originally Posted by juwan View Post
    I look over util.random and math.random, but I was't able to find anything, that would allow me to set the probability.
    Say you have a 6-sided die, and you wanted to use it to decide something with a 5/6 chance of happening in one roll. What would you do?

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Random 0/1 with given possibility

    I don't believe there is a function or utility (that I know of) which allows you to alter the probability distribution of the random output. To do so manually could get tricky but in this case is relatively easy. Get a random fraction value (using Random class posted above), skew this value by adding the appropriate probability distribution value, and cast this to an integer.

  6. The Following User Says Thank You to copeg For This Useful Post:

    juwan (November 11th, 2010)

  7. #6
    Junior Member
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Random 0/1 with given possibility

    I've solved my problem:

    p = 0,7 (user input)

    int random1 = (double) (Math.random());
    int win = 0;
    if(random1 < p){
    win = 1;
    }

  8. #7
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Random 0/1 with given possibility

    int totalOnes = 0;
    double iterations = 100000000;
    Random random = new Random();
    for (int i = 0; i < iterations; i++) {
    int zeroOrOneWithPointSevenProbabilityOfOne = random.nextInt(10) < 3 ? 0 : 1;
      if (zeroOrOneWithPointSevenProbabilityOfOne == 1) totalOnes++;
    }
    System.out.println(totalOnes + " (" + totalOnes / iterations + ")");
    db

Similar Threads

  1. Generation of random number using random class
    By JavaPF in forum Java SE API Tutorials
    Replies: 1
    Last Post: December 7th, 2011, 05:46 PM
  2. Using Random class
    By javapenguin in forum What's Wrong With My Code?
    Replies: 22
    Last Post: November 18th, 2010, 02:34 PM
  3. Generating random numbers
    By abelned in forum Object Oriented Programming
    Replies: 1
    Last Post: September 1st, 2010, 07:24 AM
  4. random
    By b109 in forum Java Theory & Questions
    Replies: 2
    Last Post: June 3rd, 2010, 04:19 AM
  5. Random numbers
    By Pooja Deshpande in forum Java SE APIs
    Replies: 8
    Last Post: June 5th, 2009, 04:36 AM