sp3cpk Posted April 5, 2017 Posted April 5, 2017 (edited) 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, 2017 by sp3cpk
Charlotte Posted April 5, 2017 Posted April 5, 2017 option == random(2) if option == 1 , do method1 else method2 3
whipz Posted April 5, 2017 Posted April 5, 2017 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
sp3cpk Posted April 5, 2017 Author Posted April 5, 2017 19 minutes ago, Charlotte said: option == random(2) if option == 1 , do method1 else method2 got it! Thanks a bunch
Vilius Posted April 5, 2017 Posted April 5, 2017 (edited) 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, 2017 by Vilius
Final Posted April 5, 2017 Posted April 5, 2017 (edited) Just remember the random can be a zero. EDIT: Villius sniped me. Edited April 5, 2017 by Final 2
sp3cpk Posted April 5, 2017 Author Posted April 5, 2017 (edited) 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, 2017 by sp3cpk
Vilius Posted April 5, 2017 Posted April 5, 2017 (edited) 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, 2017 by Vilius
Lewis Posted April 5, 2017 Posted April 5, 2017 int method; method = random(0,1); if (method == 0 { do w.e } else if (method == 1) { do w.e }
sp3cpk Posted April 5, 2017 Author Posted April 5, 2017 (edited) 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, 2017 by sp3cpk
sp3cpk Posted April 5, 2017 Author Posted April 5, 2017 (edited) 5 minutes ago, Lewis said: Didn't mean to post Edited April 5, 2017 by sp3cpk
Vilius Posted April 5, 2017 Posted April 5, 2017 (edited) 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, 2017 by Vilius
Vilius Posted April 5, 2017 Posted April 5, 2017 (edited) 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, 2017 by Vilius 1
Charlotte Posted April 5, 2017 Posted April 5, 2017 Was not really thinking about that. I don't code 1
Lewis Posted April 5, 2017 Posted April 5, 2017 47 minutes ago, Charlotte said: Was not really thinking about that. I don't code