Jump to content

Calling random method


sp3cpk

Recommended Posts

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 by sp3cpk
Link to comment
Share on other sites

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? :gnome:

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 by Vilius
Link to comment
Share on other sites

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 by sp3cpk
Link to comment
Share on other sites

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 by Vilius
Link to comment
Share on other sites

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 :D

EDIT: simply changing it to method = random(0,1); worked thanks @Lewis and @Vilius and everyone else :D

Edited by sp3cpk
Link to comment
Share on other sites

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 :D

EDIT: simply changing it to method = random(0,1); worked thanks @Lewis and @Vilius and everyone else :D

I edited my post, it was fucked up for some reason, the spoilers dont seem to work for me haha

Edited by Vilius
Link to comment
Share on other sites

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 by Vilius
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...