java - How to generate n random numbers with a sum of m in public static void main (scanner)? -


i'm trying generate 4 different numbers second else if statement (life_1.equals(a) , random integer third else if statement (life_1.equals(c) without changing public static void main myscanner. answering , sorry if question doesnt make sense @ :/

system.out.println("do want use lifeline now? (y - yes or n - no)"); answer =myscanner.nextline();   boolean life=false;  if (answer.equals("y")) { {       system.out.println("alright. 1 of lifelines use?( f-fifty-fifty  , a-ask audience, c-call friend)");      life_1= myscanner.nextline();     if (life_1.equals("f") && lifeline_5050 !=0){       system.out.println(" answer not b or d");       lifeline_5050 = lifeline_5050 - 1;      life=true;     }     else if (life_1.equals("a") && lifeline_ask != 0) {       system.out.println("the audeince answers are: a."+randomnum1+ "b. "+randomnum2+"c. "+randomnum3+ "d. "+randomnum4 );       lifeline_ask= lifeline_ask -1 ;       life=true;     }     else if (life_1.equals("c") && lifeline_call !=0 ){          system.out.println("your friend says answer "+randomnum+" possible.");       lifeline_call = lifeline_call -1;        life=true;}     else {       system.out.println("invalid answer. can't use lifeline."); }     }     while (life==false); } 

to generate n numbers sum m, can think permutation of m o , n - 1 |, count o between | , number of o numbers generated.

for example, given m = 10 , n = 4, permutation may be

o|oooo|ooo|oo 

then, results 1, 4, 3, 2.

to generate permutation, should shuffle.

you have consider position of |, implementation may this:

int[] generatenumbers(int m, int n) {     if(m < 0 || n <= 0) throw new illegalargumentexception();     if (n == 1) return new int[]{m};     int separatornum = n - 1;     int permutationlength = m + n - 1;     int[] positions = new int[separatornum];     int[] result = new int[n];     // shuffle array of o , |     (int = 0; < separatornum; i++) {         int p = (int)(math.random() * (permutationlength - i)) + i;         if (p >= separatornum) {             // o swapped | if number not swapped | before             boolean dupe = false;             (int j = 0; j < i; j++) {                 if (positions[j] == p) {                     dupe = true;                     break;                 }             }             positions[i] = (dupe ? : p);         } else {             // | swapped |, | here             positions[i] =i;         }     }     // sort positions     (int = positions.length - 1; > 0; i--) {         (int j = 0; j < i; j++) {             if (positions[j] > positions[j + 1]) {                 int temp = positions[j];                 positions[j] = positions[j + 1];                 positions[j + 1] = temp;             }         }     }     // decode positions     result[0] = positions[0];     (int = 1; < n - 1; i++) {         result[i] = positions[i] - positions[i - 1] - 1;     }     result[n - 1] = permutationlength - positions[n - 2] - 1;     return result; } 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -