-1

So I'm having a brain fart at the moment and can't figure out how to finish this part of the code:

String[] diceNumbers = {"1", "2", "3", "4", "5", "6"};
String pickedNumber = (diceNumbers[new Random()]);

I saw this on another StackedOverflow past but I can't find it again.

I want it to be stored as a String variable so I can add it into another line of code because I'm using it in a Minecraft plugin

Bukkit.broadcastMessage(p.getName() + "has rolled a" + pickedNumber);

is what I am trying to do

secon19665
  • 19
  • 1
  • 8

1 Answers1

0

Use ThreadLocalRandom:

String pickedNumber = diceNumbers[ThreadLocalRandom.current().nextInt(diceNumbers.length)];
ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
  • It would be nice to explain why this would be a solution. – AxelH Jul 03 '19 at 05:57
  • This just answered my question. I have been using ```String pickedNumber = (diceNumbers[new Random().nextInt(diceNumbers.length)]);``` – secon19665 Jul 03 '19 at 05:57
  • @secon19665 This is semantically the same but `ThreadLocalRandom` is generally faster than creating a `Random` instance every time – ZhekaKozlov Jul 03 '19 at 06:04
  • @ZhekaKozlov I'm only using it once in the plugin that I am making so it doesn't fully matter to me. I don't see why people are downvoting your answer because it answers my question haha. I will be accepting your answer! – secon19665 Jul 03 '19 at 06:05