How to Exclude a Number from a Random Number Generator

Started by Lil Shoosh on

Topic category: Help with modding (Java Edition)

Last seen on 10:24, 30. Dec 2023
Joined Apr 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How to Exclude a Number from a Random Number Generator
Wed, 07/20/2022 - 22:19 (edited)

Heyo!

 

So, for a little while now, I have been working on a mod that creates an alternative enchanting system to the Minecraft enchanting system, and moreover, adds a variety of new enchants. However, I have encountered an issue.

I am using the "random integer between "x" min and "y" max included" to generate a number that will correspond with a given enchant from a list. However, because I need the procedure to reroll and choose a different number multiple times, I need to know how to exclude a number when rerolling so that the same enchant isn't chosen more than once. For example, if the list was between numbers 1-16, and it chooses 4, the next time it rolls a number, I need the procedure to exclude 4, and thus, have it between 1-3,5-16. And when it rerolls again, and for example, chooses a 5, I need it to now exclude that number too, ie. have the next roll between 1-3,6-16. 

An alternative solution could be the following, yet I still need help. Let's say that the Sharpness enchant corresponds to the number 4. If the generator chooses the number 4, then it adds the enchant of Sharpness and sets a variable to true. For the next roll, I could have it check to see if the variable is true, and if so, then have it cancel the procedure and reroll until a different number is found. 

What could I do?

 

Have a blessed day!

Edited by Lil Shoosh on Wed, 07/20/2022 - 22:19
Set local variable to random…
Fri, 07/22/2022 - 19:08

Set local variable to random number, then check if that random number is the one you don't want and then set to another value, or alternatively, re-roll in loop until the number is not the one excluded

Last seen on 20:08, 17. Apr 2024
Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Another solution I found is…
Sun, 07/31/2022 - 13:48

Another solution I found is to just set the range of a random number to different ranges based on which numbers you want to exclude. I was making a custom GUI system, for example, where I want a randomized crafting result that excludes an item if it's in the ingredient slot. To do this I essentially made an RNG table that selects 5-25 if that item is an ingredient, and 1-25 if it isn't, with options 1-4 being said item.