# Solved [UNSOLVED] CHANCES

Discussion in 'Plugin Development' started by PickNChew, May 9, 2014.

Thread Status:
Not open for further replies.
1. Offline

### PickNChew

I've looked at multiple threads on the forums but none gave me answers. What I want to do is to check the chances listed in a config. It'd be like 1/10, 1/100, 1/1000 etc. My brain can't process how to do this... So basically, you have multiple things that the it could be. But I don't want to generate a number one after another. I want it so that 1/10, 1/100, etc. will have equal chances of happening. I don't really know if this made sense but if you need further explaining I will.

#1
2. Offline

### DxDy

I think further explanation would be useful.

As far as I understand you want different actions to happen with a different propability? The basis here would be the Java Random Number Generator.
Code:java
`Random random = new Random();double value = random.nextDouble(); // border is the propability (10% = 10/100, ...)if(value < border){  // Do the action}`

That would be the basis for anything else.

#2
3. Offline

### PickNChew

DxDy Yeah, I know how to create a random number. I'm trying to get the chance out of something like this:
Code:
```Something:
Chance: 1000
Something1:
Chance: 10000
Something2:
Chance: 100```
So then it would only do one of those things.

Any help is appreciated. Sorry if bumping isn't allowed ;?

EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: Jun 8, 2016
#3
4. Offline

### GeorgeeeHD

Get the number from the config then this....

Code:java
`Random random = new Random();int intFromConfig = getNumFromConfig;int rand = random.nextInt(intFromConfig); //Number between 0 and config chance number`

then do what DxDy said

#4
5. Offline

### PickNChew

I have multiple chances. If I were to make a for loop, there could be a chance that more than 1 is == 1.

#5
6. Offline

### minoneer

1. Save all Chances from config
2. Sum them up
3. Get a random Value between 0 and the sum
4. Sum them up again until the sum is > than your random number

There might be more efficient algorythms, but for most uses this should be fine.

#6
7. Offline

### Shadow_Parallax

1. Create a boolean, set it to false.
2. Create a for loop to iterate through all your values
3. If value x (from the for loop which is loaded from your config file) is smaller than your chance value set boolean to true
4. In the for loop (at the end) check if the boolean is true, if so
Code:java
`break;`

In the end it'd look something like this
Code:java
`Random r = new Random();boolean hasHappened = false; for(int i = 0; i < values.length; i++){if(r.nextInt() < values[ i ]){hasHappened = true;}if(hasHappened){break;}}`

EDIT: More efficient way
Code:java
` Random r = new Random();boolean hasHappened = false; for(int i = 0; i < values.length; i++){if(r.nextInt() < values[ i ]){// Do stuffbreak;}}`

PickNChew likes this.
8. Offline

### PickNChew

Yep figured it out this morning. Thanks

#8
9. Offline

No problem

10. Offline

### PickNChew

Shadow_Parallax I seem to be having some trouble. I'm trying to create an infinite for loop so I can break it later. But I need to loop through my config like this:
Code:
`for(String key : getConfig().getConfigurationSection("Rewards").getKeys(false))`
I've tried doing this in the for loop.
Code:
```if (lucky == false) {
continue;
}

if (lucky == true) {
// Do stuff
break;
}```
continue; doesn't seem to make it loop again?

Anyone please help me. I don't want to wait 24 hours to get a response on repeating a loop.

EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: Jun 8, 2016
#10
11. Offline

### Shadow_Parallax

PickNChew You don't need to do anything to make it continue. Just have a condition on which it can exit, like you have. So remove the if (lucky == false) bit.

12. Offline

### PickNChew

Yes, but I need atleast one to be selected. So if none of them == 1 one of them has to be 1.

The continue; bit was to try to make it loop again.

EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: Jun 8, 2016
#12
13. Offline

### Shadow_Parallax

PickNChew After the loop check if lucky is still false and then just set it to true. It's easier for me to help if you post your whole method here though.

14. Offline

### PickNChew

I don't want to set it to true. I want it to do it by Chance. Basically, what I'm doing is generating random numbers from 1 - something in the config. Until lucky == true it will keep looping that's what I'm trying to do.

#14
15. Offline

### Bobit

Use a while loop?
Code:java
`lucky=false;while(lucky){//keep repeating this}`

This is really basic...

#15
16. Offline

### PickNChew

I've tried using a while loop with a for loop nested into it. It crashes my server

Nevermind, I've solved it myself.

EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: Jun 8, 2016
#16
17. Offline

### Shadow_Parallax

That code would never execute.

18. Offline

### Bobit

Shadow_Parallax

Context:
"Until lucky == true it will keep looping that's what I'm trying to do."

#18
19. Offline

### Shadow_Parallax

Yeah. So it'll never execute.
while(lucky) <--- Lucky is false, while loop will never execute.
while(!lucky) <--- Lucky is false, so condition is true, while loop will execute.

20. Offline

### Bobit

#20
Thread Status:
Not open for further replies.