+ Reply to Thread
Results 1 to 2 of 2

Thread: Random Numbers

  1. #1

    Random Numbers

    I have 3 conditions and i`d like to run them based on probability.
    So what i mean is :
    Say i want to give the first condition probability of 30% , second condition probability of 50% and third condition probability of 20%.
    So that when i generate random numbers say limit is 10
    From 1 to 3 [RUN COND 1] 30%
    From 4 to 8 [RUN COND 2] 50%
    From 9 to 10 [RUN COND 3] 20%

    Any idea how can i do that ? because i have no idea how can i use random numbers.

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    41
    Here you are
    Code:
    Random r = new Random();
    r.nextInt(100);
    
    int p1 = 30;
    int p2 = 50;
    
    // chances 30%
    if ( n < p1 )
    /*COND 1 HERE*/
    
    // chances 50%
    else if ( n >= p1 && n < p1 + p2 )
    /*COND 2 HERE*/
    
    // chances 20% which is the rest
    else // the third condition
    /*COND 3 HERE*/

+ Reply to Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts