Thread: misc.random and it's uses

Page 1 of 3 123 LastLast
Results 1 to 10 of 28
  1. #1 misc.random and it's uses 
    Banned

    Join Date
    Jan 2007
    Posts
    260
    Thanks given
    3
    Thanks received
    5
    Rep Power
    0
    Just posted this Mopar, going to post here also

    Terms to know:

    Integer: An integer is any whole number (1, -9, 1049, 0, etc). There are 4 types of integer's, we will focus on just one. An int!

    Int: An int is a type of integer. Int's do have their limits, one that hopefully you never exceed . They have a minimum value of -2,147,483,648 and a maximum value of +2,147,483,647.

    __________________________________________________ ____________

    What misc.random is?

    Well, looking at the actual code is always helpful.

    Code:
    	public static int random(int range) {
    		return (int)(java.lang.Math.random() * (range+1));
    	}
    So, by looking at the code you should have concluded that:

    A) (int range) can be any number that you define: Used like
    Code:
    misc.random(1)
    B) You should see that an int is being multiplied by Math.random multiplied by the number you define (range) + 1

    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.

    __________________________________________________ _________________

    So why use this? We would use this for random events for example! We would do this like the following:

    Code:
    int event = Misc.random(3); //defines what the int is (0,1,2, or 3) and names it event
    
    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
    }
    However, if we were using a huge number like misc.random(1000) we would not want to define each number, as there would be 1001 if statements! (YUCK!)

    So to fix this we could use something similar to:

    Code:
    if (event == 327) { //States that if the int is 357 something will happen
    	c.sendMessage("The event outputs 357"); //Sends a message :P
    }
    else {
    	c.sendMessage("The event was not 357"); //All numbers that come up other then 357 will now output this message!
    }
    So, now that the basic's of this feature are understood we can put it to use!

    Under your click object case (case 132), we could add the clicking of a crate and the crate could have a few different outcomes.

    So try something like this:

    Code:
    case 357: //Crate
    				c.sendMessage("You search the crate.."); //Sends the player a message
    				int prize = Misc.random(3); //defines the int (can be any number from 0 - 4)
    			if(prize == 0) { //if the int is 0
    				c.sendMessage("You find a nasty old cat hiding in the crate!"); //sends a message
    				c.addItem(1555, 1); //Adds a cat  :confused:  :|
    				}
    			if(prize == 1) { //if the int is 1
    				int coins = Misc.random(300); //defines a new int (any number from 0 to 301)
    				c.sendMessage("You find a few dusty coins..");//sends message
    				c.getItems().addItem(995, coins);//adds an item and a random amount up to 301
    			}
    			if(prize == 2) { //if the int is 2
    				c.sendMessage("You find an odd looking garment.."); //sends message
    				c.addItem(6857); //Adds a scarf	
    			}
    			if(prize == 3) { //if the int is 3
    				c.sendMessage("You fail to find anything.."); //sends message
    			}
    				break;
    Now you have basically figured out the misc.random feature! By following this tutorial you should now be able to add many different types of random features into your server! Things such as random events, a random amount of items, or you could even base a skill such as cooking off of it!
     

  2. #2  
    Old School Member

    Join Date
    Feb 2008
    Age
    29
    Posts
    2,163
    Thanks given
    372
    Thanks received
    40
    Rep Power
    687
    Nice work love the conventions.

    Quote Originally Posted by Colby View Post
    Rofl, moron. They're ALL going to be null idiot. This is such complete bullshit, it makes me want to strangle you.
     

  3. #3  
    Banned

    Join Date
    Jan 2007
    Posts
    260
    Thanks given
    3
    Thanks received
    5
    Rep Power
    0
    Quote Originally Posted by xx k03d xx1 View Post
    Nice work love the conventions.
    Thanks for the response, and yup, conventions make programming much less time consuming and also makes it much easier to edit old code.
     

  4. #4  
    Banned

    Join Date
    Jul 2008
    Posts
    2,056
    Thanks given
    26
    Thanks received
    98
    Rep Power
    0
    Extremly basic, Interpreted well, good explanation, and converted for people who have no idea.

    Great thread, make more of these. it may just improve servers as we know it as most people dont know basics and will learn fast and well with these.

    Repped
     

  5. #5  
    Banned

    Join Date
    Jan 2007
    Posts
    260
    Thanks given
    3
    Thanks received
    5
    Rep Power
    0
    Quote Originally Posted by Jonny J View Post
    Extremly basic, Interpreted well, good explanation, and converted for people who have no idea.

    Great thread, make more of these. it may just improve servers as we know it as most people dont know basics and will learn fast and well with these.

    Repped
    Had to be as basic as possible, I wrote it for moparscapiens.

    But on a serious note, thank you
     

  6. #6  
    Registered Member


    Join Date
    May 2008
    Posts
    710
    Thanks given
    42
    Thanks received
    21
    Rep Power
    323
    Good explanation and usefull will help many people thanks for this rep++
     

  7. #7  
    Banned

    Join Date
    Jan 2007
    Posts
    260
    Thanks given
    3
    Thanks received
    5
    Rep Power
    0
    Quote Originally Posted by Problem View Post
    Good explanation and usefull will help many people thanks for this rep++
    Thank you very much
     

  8. #8  
    Registered Member
    minutes's Avatar
    Join Date
    Apr 2008
    Posts
    1,926
    Thanks given
    2
    Thanks received
    19
    Rep Power
    465
    Wanna know what is werid? however I think you say yes, When I make anything ( tut ) then so many players gona make tut to... )



    click banner to join gtracer!
     

  9. #9  
    Banned

    Join Date
    Jan 2007
    Posts
    260
    Thanks given
    3
    Thanks received
    5
    Rep Power
    0
    Quote Originally Posted by minutes View Post
    Wanna know what is werid? however I think you say yes, When I make anything ( tut ) then so many players gona make tut to... )
    I don't understand, clarify what you mean
     

  10. #10  
    Registered Member

    Join Date
    Oct 2007
    Posts
    2,413
    Thanks given
    254
    Thanks received
    479
    Rep Power
    2785
    Now explain the hexadecimal and string convertions.
    Good job, really good explanation.
     

Page 1 of 3 123 LastLast

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
  •