随机数发生器唯一 - 非重复 [英] Random Number generator unique - non repetetive

查看:98
本文介绍了随机数发生器唯一 - 非重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Experts,



我正在生成一些6位数的随机数,这应该是一个发票号码。



但我不确定6位数总是如此独特?

到目前为止它如何记住所有生成的数字并创建一个新数字。

它应该永远是唯一的......



这是我尝试的内容

 受保护的 功能 Random() As  整数 
Dim RandomClass As Random()
Dim RememberSet 作为 HashSet( 整数

Dim RandomNumber As 整数

RememberSet.Count< 5
RandomNumber = RandomClass。 Next 0 10
如果 RememberSet.Add(RandomNumber)然后
RandomNumber = RandomNumber
结束 如果
结束 虽然
返回 RandomNumber
结束 功能







有人可以帮我解释一下这个独特之处吗?到目前为止它是如何记住所有生成的数字并且它不重复它。



例如:几个月前它产生的数字就像522689



再过n个月之后它不应该生成相同的522689号。

解决方案

这个函数没什么意义。每次调用它时,它会生成5个数字,它知道它在调用时生成的数字中是唯一的,一旦生成了5个数字,它就会返回生成的最后一个数字,忽略第一个数字。



它没有能力确保数字在全球范围内是独一无二的,即它今天不会选择一个昨天没有选择的数字。


使用随机数字,即使不太可能附加,也不可能保证唯一性和非重复性。



保证唯一性和非重复性,最简单是序号。



您是否正在使用您提供的代码?

据我所知,您的代码生成1个数字数字不是你想要的。



你能告诉我们你需要随机数的原因吗?


如何您是否可能期望来自随机号码生成器的唯一性?难道你不觉得这样的特征会严重偏向随机分布吗?当然,不保证唯一性,期限。



要选择一些随机值的唯一集合,您可以过滤掉重复的值。在某些上下文中执行此操作的一种有效方法是将所有唯一值放在类的实例(在相同的上下文中) System.Collections.Generic.HashSet<>

HashSet(T)Class( System.Collections.Generic) [ ^ ]。



对于每个随机生成的值,如果已经在集合中,则首先检查它,然后添加它或继续生成随机数,直到找到一个新的唯一。具体如下:

HashSet(T) .Contains Method(T)(System.Collections.Generic) [ ^ ],

HashSet(T).Add Method(T)(System.Collections.Generic) [ ^ ]。



注意它与错误代码的不同之处;解决方案1中解释了它的谬误。



现在,正确使用类的两条建议 System.Random



  1. 在整个应用程序中更准确地使用它的一个实例,并在应用程序域中使用它。在整个代码中共享此引用(例如,将其传递给不同的方法)。
  2. 使用种子初始化实例。使用此构造函数:

    随机构造函数(Int32) (系统) [ ^ ]。





最后看一下你的命名: RandomClass 。在类的名称中使用class这个词已经很愚蠢,但这甚至不是一个类,这就是实例。



- SA

Hello Experts,

I am working generating some 6 digit Random Number and that should be a Invoice Number.

But I am not sure how is that 6 digit going to be Unique always?
How does it remember all the generated Numbers so far and create a new one.
It should be always unique ...

Here is what I have tried

Protected Function Random() As Integer
    Dim RandomClass As New Random()
    Dim RememberSet As New HashSet(Of Integer)

    Dim RandomNumber As Integer

    While RememberSet.Count < 5
        RandomNumber = RandomClass.Next(0, 10)
        If RememberSet.Add(RandomNumber) Then
            RandomNumber = RandomNumber
        End If
    End While
    Return RandomNumber
End Function




Can someone help me in explaining how is this Unique? how does it remember all the generated numbers so far and it doesn't repeat it.

Ex: Few months back it generate a number like 522689

After n no of months again it shouldn't generate same 522689 Number.

解决方案

The function makes little sense. Every time it is called it generates 5 numbers that it knows to be unique among the numbers it has generated that time it was called, and once it has generated 5 numbers it returns the last number generated, ignoring the first 4.

It doesn't have the ability to ensure numbers are globally unique, ie that it won't pick a number today that it didn't pick yesterday.


With random numbers, it is impossible to guaranty uniqueness and non repetitive, even if unlikely to append.

To guaranty uniqueness and non repetitive, the easiest is sequential numbers.

Are you using really the piece of code you provided ?
As I understand it, your code generate a 1 figure number which is not what you want.

Can you give us the reason of your need for a random number ?


How could you possibly expect uniqueness from a random number generator? Don't you see that such "feature" would badly bias the "random" distribution? Of course, uniqueness is not guaranteed, period.

To choose unique set of some random values, you can filter out repeated values. One efficient way to do this in some context, it to put all unique values in the instance (in the same context) of the class System.Collections.Generic.HashSet<>:
HashSet(T) Class (System.Collections.Generic)[^].

With each randomly generated value, you first check up it if is already in the set, and then either add it or keep generating random numbers until you find a new unique on. This is how:
HashSet(T).Contains Method (T) (System.Collections.Generic)[^],
HashSet(T).Add Method (T) (System.Collections.Generic)[^].

Pay attention how it is different from your wrong code; it's fallacy is explained in Solution 1.

Now, two recommendations for correct use of the class System.Random:


  1. Use only one instance of it in your whole application, more exactly, and application domain. Share this reference throughout your code (say, pass it to different methods).
  2. Initialize the instance with seeding. Use this constructor:
    Random Constructor (Int32) (System)[^].



Finally look at your naming: RandomClass. Using the word "class" in a name of a class would already be silly, but this is not even a class, this is the instance.

—SA


这篇关于随机数发生器唯一 - 非重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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