How to get random unique numbers between any 2 numbers in c# without storing and checking the previous output?
Please see the below link for an example:-
https://adf.ly/msz2d[^]
Thanks in advance
解决方案Hi Agen_Spock,
It seems you are not getting it (concluded from your variouse comments)...
If you don't storewhich numbers (of any set you wish your random numbers to come from e.g. 1-50) are already used, you can not knowif the number is unique- Can you agree on that?
So your whole "requirenment" "without storing values and checking..." is wrong. It can't be done- this is a pure logical contradiction and has nothing to do with programming in general.
Yes there are algorithms which will make it very unlikelyto produce the same number again (e.g. GUIDs).
So a valid solution could be: Pre-Generate a "set" of numbers - like playing cards and pick them randomly. If you try this you have to think about what happens if you "run out" of numbers/cards.
Another (may be inefficient) approach could be just to store all the generated numbers from past calls and check if the new number is contained - then retry if number was used - again what happens if there are no more "free" numbers in range...
So you see, just insisting on a "not so well thought" requirement won't solve your problem. Maybe you tell us what you want to achieve in the end and what is your real problem?
Kind regards
Johannes
As other as mentionned, it is essentially impossible to achieve that. Either, you have to use a know sequence that happen to be unique (in that case you still need to know where you are).
It would be possible to do it wihout storing previous values if you use a seed for the random generator and an index for the number of call so that you can regenerate old values and check if the new value is unique.
In that case, your performance would drop a lot for long sequences (thousand of items) but it will use less memory.
But you still have to check previous output.
Well, you can generate sequence that are not so random using permutations with fixed offset so that it is known that you will get all numbers but result won't be so random.
For example, if you want number betwen 0 and 50, you can do something like adding 7 at each step and remove 50 when it is above 50.
0, 7, 14, 21, 28, 35, 42, 49, 6, 13, 20...1, 8, 15, 22, 29, 36, 43, 50 and at that point, the sequence will start over at 7 (or 0 if you uses modulo when above 50 instead of substraction).
But in general term it is impossible to achive whant you ask.
Technically, there is a Next method on the Random class which allows you to specify bounds for your random number.
Here: Random.Next Method (Int32, Int32)[^]
Beware!
- The first parameter (minValue) is inclusive, whereas the second (maxValue) is exclusive.
It means that, if you want a number between 1 and 50, you would have to write:
Random r = new Random();
int randomNumber = r.Next(1, 51);
There subsists an issue, though: this will give you a random number, but will not assure you its uniqueness in any way.
For that, there still have been answered to you that the Guid struct is much more appropriate.
More informations here: Guid Structure[^]
Hope this helps.