在c#中获取随机唯一编号 [英] Get Random Unique Number in c#

查看:616
本文介绍了在c#中获取随机唯一编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c#中的任意两个数字之间获取随机唯一数字,而存储并检查以前的输出



请参阅下面的内容链接示例: -

https://adf.ly/msz2d [ ^ ]





提前谢谢

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

推荐答案

嗨Agen_Spock,



看来你没得到它(从你的variouse评论)...



如果你不存储哪些数字(任何你希望你的随机数来自哪个数字)例如1-50)已经被使用,你可以不知道如果数字唯一 - 你能同意吗?



所以你的整个需求没有存储值和检查......是错误的。 无法完成 - 这是一个纯粹的逻辑矛盾,与一般的编程无关。



是的,有算法这将使不太可能再次产生相同的数字(例如GUID)。



所以一个有效的解决方案可能是:预生成一组数字 - 就像打牌一样随机挑选。如果你试试这个,你必须考虑如果你用完数字/卡会发生什么。



另一种(可能效率低下)方法可能只是存储来自过去通话的所有生成的数字并检查是否包含新号码 - 然后在使用号码时重试 - 再次如果在范围内没有更多免费号码会发生什么...



所以你看,只是坚持不太好想的要求并不能解决你的问题。也许你告诉我们你最终想要达到什么目的,你真正的问题是什么?



亲切的问候



Johannes
Hi Agen_Spock,

It seems you are not getting it (concluded from your variouse comments)...

If you don't store which numbers (of any set you wish your random numbers to come from e.g. 1-50) are already used, you can not know if 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 unlikely to 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


正如提到的那样,基本上不可能实现这一点。或者,你必须使用恰好独特的知道序列(在这种情况下你仍然需要知道你在哪里)。



这是可能的如果您使用随机生成器的种子和调用次数的索引,它可以存储以前的值,以便您可以重新生成旧值并检查新值是否唯一。



在这种情况下,对于长序列(数千项),你的表现会下降很多,但会减少使用内存。



但是你还是要检查以前的输出。



好​​吧,你可以使用固定偏移量的排列生成不那么随机的序列,这样就可以知道你将获得所有数字但是结果赢了不是那么随意。



例如,如果你想要0到50之间的数字,你可以做一些事情,比如在每一步加7,然后去掉50 50以上。



0,7,14,21,28,35,42,49,6,13,20 ...... 1,8,15,22 ,29, 36,43,50,此时,序列将从7开始(如果你使用模数超过50而不是减法,则为0)。



但是在一般来说,你不可能实现这一点。
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.


从技术上讲,上有一个 Next 方法随机类,允许您指定随机数的边界。

这里:Random.Next方法(Int32,Int32) [ ^ ]



小心!



- 第一个参数( minValue )包含在内,而第二个参数( maxValue )是独家

这意味着,如果你想要1到50之间的数字,你必须写:

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);





但存在一个问题:这会给你一个随机数,但不会以任何方式保证你的独特性。

为此,仍然有人回答你 Guid 结构是更合适。

更多信息: Guid Structure [ ^ ]



希望这有帮助。



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.


这篇关于在c#中获取随机唯一编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆