Jump to content
View in the app

A better way to browse. Learn more.

OSBot :: 2007 OSRS Botting

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Calling random method

Featured Replies

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

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

  • Author
19 minutes ago, Charlotte said:

option == random(2)

if option == 1 , do method1

else

method2

got it! Thanks a bunch :D

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

Just remember the random can be a zero.

EDIT: Villius sniped me.

Edited by Final

  • 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 by sp3cpk

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

int method;

method = random(0,1);
 

if (method == 0 {

do w.e
} else if (method == 1) {

do w.e

}

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

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

Edited by sp3cpk

  • Author
5 minutes ago, Lewis said:

 

Didn't mean to post :(

Edited by sp3cpk

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

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

47 minutes ago, Charlotte said:

:o Was not really thinking about that. 

I don't code :doge:

:???::???:

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.