[Basic Tutorial] Random chances of happening

Discussion in 'Resources' started by Jaker232, Nov 28, 2011.

Not open for further replies.
1. Offline

Jaker232

Code:java
` Random random = new Random();int Chance = random.nextInt(200); `

This will import a random utility that you can generate numbers from 0 to 200. You can change 200 to any number you want out of it.

Code:java
` if(Chance >= 100) {} `

The >= is in mathematical statement, "greater than or equal to". So if our generated number was 137, the plugin will read it like..
Code:
```137 > 100
```
which passes it onto the next block of code. Let's see what we can do with this. Here's an example.

Code:java
` if(Chance >= 100) {    player.sendMessage(ChatColor.GREEN+"You won the lottery!");} `

If you want, you can make an else function.

Code:java
` if(Chance >= 100) {    player.sendMessage(ChatColor.GREEN+"You won the lottery!");} else {    player.sendMessage(ChatColor.RED+"No. You didn't win the lottery. D:");} `

You can do a bunch of stuff with this. Be creative, my friends.

All codes but examples are copied from CrazySword, copyrighted 2011.

#1
BaHeTo0, L3N and devilquak like this.
2. Offline

Forge_User_15303315

Nice tutorial, this will probally help some people.

3. Offline

Jaker232

Thanks for the nice comment.

#3
4. Offline

Nice man.
Could also use
Code:
```if ((int) (Math.random() * 4) == 0) {
//Stuff
}
```

#4
5. Offline

iffa

I face palmed a bit in rl. Pretty basic java here...

#5
6. Offline

Sleaker

Just so people don't get confused - the easiest way to do percentages is with < not >= (as it requires some extra math)
Example using the random variable from above:
Code:
```if (chance < random.nextInt(100)) {
// Do Stuff!
}
```
The reason why you don't want to do >= is because as chance increases there should be a higher chance of success.

Code:
```if (chance >= random.nextInt(100)) {
// Do Stuff
}
```
The above code will actually have a less chance of success (inverse) - lets say you wanted a 90% chance of success, using the above code there will actually only be a 10% chance of success.

One thing to not forget, random.next will always include 0 and exclude max. that means random.nextInt(100) will NEVER return 100, but will have a chance to return 0. So using the first code example is the proper (my opinion) way to check % chance directly via integers.

#6
7. Offline

Jaker232

Changed the title. You happy now?

#7
8. Offline

Forge_User_73049341

It may be basic to you and me, but their are other people out there that would find this useful. We all started from somewhere.

WalkerCrouse likes this.
9. Offline

Forge_User_55332298

• There is a whitespace after if.
• There is a trailing and leading whitespace after the "+" operator.
I know you will be upset because I am posting this, but should also help making other people's code readable.

10. Offline

this.

11. Offline

+1

12. Offline

SkillEscalation

I'm learning java right now and I did find that learning the syntax from this was helpful. Has a few errors as stated above, but overall it helped me. Thanks!