Thread: misc.random and it's uses

Page 3 of 3 FirstFirst 123
Results 21 to 28 of 28
  1. #21  
    Registered Member
    Naughty Panda's Avatar
    Join Date
    Dec 2007
    Posts
    631
    Thanks given
    12
    Thanks received
    46
    Rep Power
    347
    As much as I think this is a very well explained tutorial, looking at the method:

    Code:
    	public static int random(int range) {
    		return (int)(java.lang.Math.random() * (range+1));
    	}
    I don't think this part of it was correct:

    So if you were to use misc.random(3) the following numbers would be used and outputted.

    1 x 0 = 0
    1 x 1 = 1
    1 x 2 = 2
    1 x 3 = 3

    This is because it takes the integer (1) and multiplies it by a random number, up to 3 in this case, and outputs the result.
    As the method Math.random() is a static double, which returns a value 0.00 < (x) < 1.00.

    So it actually takes the range you specify, in your example 3, and increases it by 1, and then multiplies it by the random double generated and takes the integer part of the result, for example:

    Misc.random(3) =
    0.47 * (3+1) = 1.88
    (int)1.88 would return 1.

    Correct me if I'm wrong, but I'm sure thats how the method works .
     

  2. #22  
    sυввч

    Sub's Avatar
    Join Date
    Aug 2007
    Age
    24
    Posts
    4,352
    Thanks given
    1,205
    Thanks received
    359
    Rep Power
    2845
    Before
    Code:
    if (event == 0) { //States that if the int (event) is 0 something will happen
    	c.sendMessage("The event outputs 0"); //Sends a message :P
    }
    if (event == 1) { //States that if the int (event) is 1 something will happen
    	c.sendMessage("The event outputs 1"); //Sends a message :P
    }
    if (event == 2) { //States that if the int (event) is 2 something will happen
    	c.sendMessage("The event outputs 2"); //Sends a message :P
    }
    if (event == 3) { //States that if the int (event) is 3 something will happen
    	c.sendMessage("The event outputs 3"); //Sends a message :P
    }
    After
    Code:
    int event = Misc.random(3); //defines what the int is (0,1,2, or 3) and names it event
    switch(event) {
    case 0:
    c.sendMessage("The event outputs 0"); //Sends a message :P
    break;
    case 1:
    c.sendMessage("The event outputs 1"); //Sends a message :P
    break;
    case 2:
    c.sendMessage("The event outputs 2"); //Sends a message :P
    break;
    case 3:
    c.sendMessage("The event outputs 3"); //Sends a message :P
    break;
    }
     

  3. #23  
    Donator

    tj007razor's Avatar
    Join Date
    Feb 2007
    Posts
    895
    Thanks given
    2
    Thanks received
    9
    Rep Power
    210
    Why do we use Math for randoms when we could use the java.util.Random class?

    Sun has even said "Random.nextInt is both more efficient and less biased than Math.random() * n"

    Not to mention that Math.random() uses Random.nextDouble() internally.
     

  4. #24  
    Banned

    Join Date
    Jan 2007
    Posts
    260
    Thanks given
    3
    Thanks received
    5
    Rep Power
    0
    Quote Originally Posted by Skiiii View Post
    As much as I think this is a very well explained tutorial, looking at the method:

    Code:
    	public static int random(int range) {
    		return (int)(java.lang.Math.random() * (range+1));
    	}
    I don't think this part of it was correct:



    As the method Math.random() is a static double, which returns a value 0.00 < (x) < 1.00.

    So it actually takes the range you specify, in your example 3, and increases it by 1, and then multiplies it by the random double generated and takes the integer part of the result, for example:

    Misc.random(3) =
    0.47 * (3+1) = 1.88
    (int)1.88 would return 1.

    Correct me if I'm wrong, but I'm sure thats how the method works .
    Your right, I was talking to Tarok, from Mopar the other day, and he clarified that for me. But, like you said, the numbers are rounded down, so the outputs I listed are true.
     

  5. #25  
    HazeyScape
    Guest
    Delta Has Alot Of Teles using
    Code:
    misc.random(3);
     

  6. #26  
    Banned

    Join Date
    Jan 2007
    Posts
    260
    Thanks given
    3
    Thanks received
    5
    Rep Power
    0
    Quote Originally Posted by HazeyScape View Post
    Delta Has Alot Of Teles using
    Code:
    misc.random(3);
    That is so each tele isn't always in the same spot.
     

  7. #27  
    Registered Member
    Join Date
    May 2016
    Posts
    5
    Thanks given
    0
    Thanks received
    0
    Rep Power
    0
    Thanks, this is a good guide. One dumb question, is it possible define a range like misc.random(1, 6) for 1 to 6

    Commands.java
    Code:
    public static void playerCommands(Client c, String playerCommand) {
        if (playerCommand.startsWith("roll6") ){
            c.forcedChat("*Rolling D6*... I ROLLED A "+ Misc.random(6) +"!");
            }
        }
    This works however you have a 1 in 6 chance (kinda) of rolling a 0 which I don't want..
    I know that Misc.random(5) would work and just treat 0 as 6 but that's kinda lame.
     

  8. #28  
    Community Veteran


    Arch337's Avatar
    Join Date
    Sep 2008
    Posts
    2,950
    Thanks given
    210
    Thanks received
    349
    Rep Power
    1376
    Quote Originally Posted by MobCat View Post
    Thanks, this is a good guide. One dumb question, is it possible define a range like misc.random(1, 6) for 1 to 6

    Commands.java
    Code:
    public static void playerCommands(Client c, String playerCommand) {
        if (playerCommand.startsWith("roll6") ){
            c.forcedChat("*Rolling D6*... I ROLLED A "+ Misc.random(6) +"!");
            }
        }
    This works however you have a 1 in 6 chance (kinda) of rolling a 0 which I don't want..
    I know that Misc.random(5) would work and just treat 0 as 6 but that's kinda lame.
    Make a new function with this in mind. Also this tutorial is quite old, should of just made a new thread under the help section.
    Code:
    public static int random(int low, int high) {
    return low + random(high - low);
    }


    "A fail act is something you do regular, but a dumb act is something you can learn from"
    Spoiler for Problem?:
     

Page 3 of 3 FirstFirst 123

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •