Ok so i want to generate random numbers from 0 – 99 but lets say i dont want the random number to be e.g. 56. How do i generate it knowing 100% i wont get 56 but a random number from 0 – 99. In what i am doing i actually want to have multiple numbers that it doesnt pick so just doing Random.Next(0,55) Random.Next(57,99) wont work with more numbers. Thanks

Based on what you’re saying in other comments, the best way would be a random bag algorithm. You have a list with all the valid numbers you want and you get an item at a random index. You can manipulate this list to change the odds of certain numbers appearing.

It just won’t be very practical if you have a very large set of valid numbers.
Ok thanks for advice

How would you do it in real life? For example, you have a dice, and you want to generate random numbers from 1-6 one at a time, until you only have 1 unused number?

A lot of times solutions are just real-world algorithms, with optimizations put in.
There is no other way to find out whether a number has been picked unless you ….

Now, follow this line of thinking and hopefully you will find a solution, and realize that there are some more problems. There are the interesting problems!

I would roll a five sided dice..? Someone else put that it keeps generating untill i != And then the list of numbers youcdont want picked. This works but surely there is a very small possibility this will go on forever if it keeps picking the number you dont want

Create a list of int with the possible values.
Generate a random number based on the length of the list, remove the item from the list, repeat
Ok thank you

It looks like what you really want is to get all the numbers within a range in random order. One thing you could do is create a list of all possible values and randomly shuffle it. Then you can iterate through to get the items in random order but without any repeating.

Here’s an example which prints all numbers from 1 to 100 in random order without repeating:
Create a new method something like,
Public int myRandomGenerator(int NumberYouDontWhant) {
Bool exit = false;
While(exit == false) {
number = Random.next(99) ;
If(number! = numberyoudontwhant) { exit = true”
} }
return number;
}

Then just call it myresult = myRandomGenerator() ;
Basically the while loop should keep executing for another number if it has the number you don’t whant.
Sorry on mobile so excuse formatting of code.
No problem. Thanks for suggestionđź‘Ť
Something like this? (I’ve made 2 variants, one with single number to ignore, and other with list of numbers to ignore).
Also note that random.Next(0, 100) gives a random number from 0-99 not 0-100.

Edit: Actually u/throwaway_lunchtime might have better solution.
69? Nice.
I am a bot lol.
Ok thank you for help
Something like this:

The list of allowed numbers can then be manipulated, for example, so that all picked numbers are removed from the list. However, this code does not.
I would just have a function that calls it until it is not a blacklisted value and returns the value. Could be more optimized but that’s an easy solution that shouldn’t really hurt anything
Something like the following?
int myRandom;Do { myRandom=Random.Next(0,99); } While myRandom != 56;

Yes something like this but this is in theory infinate never ending. When i get to 98 numbers being taken and it can only generate 2, it would take a long time to get to those numbers no?

source