# Solved Getting a random number

Discussion in 'Plugin Development' started by Forword, Apr 23, 2018.

Not open for further replies.
1. Offline

### Forword

How would you get 2 random numbers that both add up to 100?

I currently have:

[/code]
public static int getRandom(int lower, int upper)
{
Random random = new Random();
return random.nextInt(upper - lower + 1) + lower;
}
[/code]

But that doesn't really work with what I'm trying to get.

#1
2. Offline

### JanTuck

Do you want two numbers or just 1?

Sent from my iPhone using Tapatalk

#2
3. Offline

2 numbers

#3
4. Offline

### JanTuck

So if the random number was 19, the other number would be 81?

Sent from my iPhone using Tapatalk

#4
5. Offline

yea

#5
6. Offline

### Zombie_Striker

@Forword
int upper = Math.random*100;

int lower = 100-upper;

#6
JanTuck likes this.
7. Offline

### Forword

Ok lemme test that @Zombie_Striker

@Zombie_Striker
Where would you like me to put that beacause its saying:
random cannot be resolved or is not a field

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

#7
8. Offline

### Forge_User_08244060

@Forword

Code:
```    public static int[] getNumbers(int lower, int upper)
{
Random random = new Random();
int num_1 = random.nextInt(upper - lower + 1) + lower;
return new int[] {num_1, upper-num_1};
}```
for instance: If you type
Code:
`int[] result = getNumbers(0, 100);`
and the num_1 will set to 39, then the num_2 will be (upper-39) = (100-39) = 61
as result. result[0] = 39 and result[1] = 61.

//DISCLAIMER// This is an elaborated code of @Zombie_Striker's example.

I hope this helps you out.

Last edited: Apr 23, 2018
9. Offline

### Forword

Thank you very much, that is what I wanted

@DanielTheDev
EDIT: If I were to use that in 2 lines what would I have to do?

lore1.add(ChatColor.GREEN + "Success Rate: " + getRandom(1, 50) + ChatColor.GREEN + "%");
lore1.add(ChatColor.RED + "Destroy Rate: " + getRandom(1, 50) + ChatColor.RED + "%");

Each line will have a number and
the numbers have to equal 100 :/

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

Last edited by a moderator: Apr 23, 2018
#9
10. Offline

### Forge_User_08244060

@Forword

I think you mistunderstood my code.
The method will create 2 numbers (array).

This will be the right code for you.
Code:
```int[] result = getRandom(1, 50);

lore1.add(ChatColor.GREEN + "Success Rate: " + result[0] + ChatColor.GREEN + "%");
lore1.add(ChatColor.RED + "Destroy Rate: " + result[1] + ChatColor.RED + "%");```
I think this solves your problem

11. Offline

### JanTuck

Random instances are quite wasteful please use ThreadLocalRandom to get over this.

This function will do the same exact thing.
Code:
```    public static int[] getNumbers(int lower, int upper) {
int[] returnVal = new int[2];
returnVal[1] = upper - returnVal[0];
return returnVal;
}```

#11
12. Offline

### Forword

@JanTuck
For your code, how would it work?

EDIT: Like when your using it in a message, how would it work?

#12
13. Offline

### JanTuck

The same way his code worked.

Code:
```int[] chances = getNumbers(1, 50);
lore1.add(ChatColor.GREEN + "Success Rate: " + chances[0] + ChatColor.GREEN + "%");
lore1.add(ChatColor.RED + "Destroy Rate: " + chances[1] + ChatColor.RED + "%");```

#13
14. Offline

### Forword

Alright thanks, Ill test it out in a bit

@JanTuck, the numbers it gave me were always 57 and 43

Heres my code:

Code:
```public static int[] getNumbers(int lower, int upper) {
int[] returnVal = new int[2];
returnVal[1] = upper - returnVal[0];
return returnVal;
}
int[] chances = getNumbers(1, 50);

lore.add(ChatColor.GREEN + "Success Rate: " + chances[0] + ChatColor.GREEN + "%");
lore.add(ChatColor.RED + "Destroy Rate: " + chances[1] + ChatColor.RED + "%");
```
EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: Apr 23, 2018
#14
15. Offline

### JanTuck

It can’t be unless you made the upper 100

Sent from my iPhone using Tapatalk

#15
16. Online

@Forword code blocks are [code] <code here> [/code]
Not [/code] <code here> [/code]

#16
17. Offline

### Forword

@JanTuck
When I make the upper 100, it still is 57, 43

EDIT: I need those numbers to be random everytime
To elaborate more on that, whenever I try to get 2 different numbers,
they always turned out be to 57 and 43

Last edited: Apr 23, 2018
#17
18. Offline

### JanTuck

Then get another pair of random numbers each time and update the lore

#18
19. Offline

### Forword

@JanTuck
Is there an efficient way to constantly get 2 different numbers which add up to 100?
Ex.
50, 50 | 25,75
Those numbers will constantly be changing. The code which you has provided me with, only get a combination of 2 of the exact same numbers each time. I'm trying to use this for a success and destroy rate for a custom enchantment plugin.

Thanks, Forword

#19
20. Offline

### Blackwing_Forged

@Forword
You could generate 1 number between 1 and 100 and then to get the second number do
int secondNum = 100 - randNum;

21. Offline

### Forword

@Blackwing_Forged
if you mind, can you make a piece of code for that?
I’m only asking this because whenever I try doing it, it never works.

#21
22. Offline

### Blackwing_Forged

@Forword
Can you show me your current code

23. Offline

### Forword

Code:
```public static int getRandom() {
Random random = new Random();
int Failure = random.nextInt(1-100);
int Success = 100 - Failure;
return Success;
}
```
How do I get failure number?

#23
24. Offline

### Forge_User_08244060

Code:
```public static int[] getRandom() {
int Success = 100 - Failure;
return new int[] {Success, Failure};
}

int[] both = getRandom();
int success = both[0];
int failure= both[1];
```

25. Offline

### Forword

@DanielTheDev

Code:
```package me.forword.server.commands;

import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

public class Test implements CommandExecutor {

public static int[] getRandom() {
int Success = 100 - Failure;
return new int[] {Success, Failure};
}
int[] both = getRandom();
int success = both[0];
int failure= both[1];

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {

sender.sendMessage(ChatColor.GREEN + "Success Rate:" + both[0]);
sender.sendMessage(ChatColor.RED + "Failure Rate:" + both[1]);
return true;

}
}
```
Is there a reason why that wont work?

#25
26. Online

@Forword Use that code in the onCommand, not outside where it only gets called once.

#26
27. Offline

### JanTuck

Why are you running NextInt on -99?

#27
28. Offline

### Forword

@timtower
it doesnt work when i put it inside the onCommand

#28
29. Offline

### JanTuck

Because the method needs to be outside, the getter inside.

#29
30. Offline

### Forword

@JanTuck
can you think of another way to get 2 numbers that will always reach 100?

#30