April 5, 20178 yr Hey guys! Can anyone help me with this? I've been stuck for a while.. So I'm trying to figure out how to make it so when my condition calls on a state that case will have two methods in it and one method is selected at random Example: if this is my condition do that case that: method1(); method2(); So my question is how would I make it select method1() or method2() at random, a 50% chance? Edited April 5, 20178 yr by sp3cpk
April 5, 20178 yr 6 minutes ago, Charlotte said: option == random(2) if option == 1 , do method1 else method2 was just about to say that you can also turn it into a switch case
April 5, 20178 yr Author 19 minutes ago, Charlotte said: option == random(2) if option == 1 , do method1 else method2 got it! Thanks a bunch
April 5, 20178 yr 1 hour ago, Charlotte said: option == random(2) if option == 1 , do method1 else method2 Yeah and when it returns a 0 what are you going to do then? The chance would be 1/3 instead of 1/2 to get 1 or 2 Correct code would be: switch(random(2)){ case 0: method1(); break; case 1: method2(); break; } Edited April 5, 20178 yr by Vilius
April 5, 20178 yr Just remember the random can be a zero. EDIT: Villius sniped me. Edited April 5, 20178 yr by Final
April 5, 20178 yr Author 9 minutes ago, Final said: Just remember the random can be a zero. EDIT: Villius sniped me. I implemented the code... however, i did option == random(1) it always generates a 1?;o how come? but when i leave it at 2 it'll generate a 0-2? I tried a switch case too same issue ;o? @Vilius Edited April 5, 20178 yr by sp3cpk
April 5, 20178 yr 33 minutes ago, sp3cpk said: I implemented the code... however, i did option == random(1) it always generates a 1?;o how come? but when i leave it at 2 it'll generate a 0-2? I tried a switch case too same issue ;o? @Vilius Random doesn't mean that it scatters everything evenly, flipping a coin a 100 times and getting tails 99 times out of 100 is still random, flip was random and the result was random. Changing it to the examples that I posted below might not do any difference. If you want something different you can try: if((Math.random()<0.5)? false: true) method1(); else method2(); result would be after 100 iterations (true being first method is called, false being method2) true, true, false, true, false, true, false, true, false, true, false, false, false, false, false, false, false, true, false, true, true, false, false, false, true, true, false, true, false, true, false, true, true, true, true, false, true, false, false, false, true, true, true, false, false, false, true, true, true, true, true, true, false, false, true, true, true, false, false, true, false, false, true, true, true, true, true, false, true, false, false, true, true, false, false, false, true, false, true, false, true, false, false, false, true, false, true, false, false, false, true, false, false, false, true, true, true, false, true, false, If you want to use something else then do: Random random = new Random(); switch(random.nextInt(2)){ case 0: method1(); break; case 1: method2(); break; } //OR Random random = new Random(); if(random.nextBoolean()) method1(); else method2(); results after 100 iterations: 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0 Edited April 5, 20178 yr by Vilius
April 5, 20178 yr int method; method = random(0,1); if (method == 0 { do w.e } else if (method == 1) { do w.e }
April 5, 20178 yr Author 5 minutes ago, Vilius said: Random doesn't mean that it scatters everything evenly, flipping a coin a 100 times and getting tails 99 times out of 100 is still random, flip was random and the result was random. If you want something different you can try: if((Math.random()<0.5)? false: true) method1(); else method2(); result would be after 100 iterations (true being first method is called, false being method2) Reveal hidden contents false false false true true false false true false true true false false true false false false false false false true true true false true true true false false false false true false false false false false true false true true true false true true false false true true true false true true false false true true true false true false true true true true false true false true true false true false true false true false true false true false false false true true true false false true true false false false false false false true false false false If you want to use something else then do: Random random = new Random(); switch(random.nextInt(2)){ case 0: method1(); break; case 1: method2(); break; } results after 100 iterations: Reveal hidden contents 0 1 0 1 0 0 1 0 0 0 1 0 1 0 1 0 0 0 0 1 1 0 0 0 1 1 0 0 1 0 0 1 1 0 1 0 0 1 1 0 1 0 0 1 0 1 1 1 1 1 1 0 1 1 0 1 1 0 0 1 0 1 1 1 0 0 1 1 1 1 1 0 0 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 1 0 0 0 1 1 0 1 0 0 0 Oh i totally understand! I tested it for a while :P and never got 2 like over 100 results... I will give that a try thanks EDIT: simply changing it to method = random(0,1); worked thanks @Lewis and @Vilius and everyone else Edited April 5, 20178 yr by sp3cpk
April 5, 20178 yr Author 5 minutes ago, Lewis said: Didn't mean to post Edited April 5, 20178 yr by sp3cpk
April 5, 20178 yr 24 minutes ago, sp3cpk said: Oh i totally understand! I tested it for a while :P and never got 2 like over 100 results... I will give that a try thanks EDIT: simply changing it to method = random(0,1); worked thanks @Lewis and @Vilius and everyone else I edited my post, it was fucked up for some reason, the spoilers dont seem to work for me haha Edited April 5, 20178 yr by Vilius
April 5, 20178 yr I have to correct myself, my first example should work with random(2) instead of random(1), because random(int i) is generating a number exclusively, meaning it generates a number between 0-1 and never returns a 1, doing random(2) would generate 0-1 without including the number 2. random(int min, int max) is inclusive meaning that it will include the numbers defined in the parameters. Example: random(1, 3) would generate 1, 2, 3. Edited April 5, 20178 yr by Vilius
Create an account or sign in to comment