如何使真正的随机? [英] How to make true random?

查看:87
本文介绍了如何使真正的随机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

生成随机值时出现问题.该数字显示两倍甚至更多.
例如,Random(1,10)连续4次,结果为:
1.随机值:2
2.随机值:8
3.随机值:4
4.随机值:4

我需要的是没有重复的Random值,例如:
1.随机值:2
2.随机值:6
3.随机值:4
4.随机值:8

这是我的代码:

I have a problem when generate a random value. The number shown twice even more.
For example, Random(1,10) for 4 times, and the result be:
1. Random value: 2
2. Random value: 8
3. Random value: 4
4. Random value: 4

What I need was there''s no duplicate Random value like this :
1. Random value: 2
2. Random value: 6
3. Random value: 4
4. Random value: 8

Here my code :

For i = 1 To PopuMain.members_count
         ReDim PopuMain.Members(i).Genome(PopuMain.GenoLength)
         For j = 1 To PopuMain.GenoLength 
             PopuMain.Members(i).Genome(j) = Rand_between(1, 512)
         Next j
     Next i
 End Sub

 Public Function Rand_between(ByRef Minimum As Integer, ByRef Maximum As Integer) As Double
     Randomize()
     Rand_between = Int((Maximum - Minimum + 1) * Rnd() + Minimum)
End Function 



抱歉,如果我的英语不好^ _ ^



Sorry if I''ve an bad english ^_^

推荐答案

您想要的东西不被称为真正的随机".您只需要一个伪随机数组或具有随机整数且无重复的列表.

使用某些可以保证唯一性的容器,例如System.Collections.Generic.Dictionary.添加所需数量的随机密钥,这些随机密钥是通过您已经使用的相同方法Random生成的,并尝试将每个值插入为一个密钥.如果密钥已经在字典中,只需生成另一个密钥.确保您不会无限地重复此过程,当您已经添加了所使用的Random范围内的所有可能的键时,就会发生这种情况.

就这样.

-SA
What you want is not called "true random". You just need a pseudo-random array or list with random integers without repetitions.

Use some container which can guarantee uniqueness, such as System.Collections.Generic.Dictionary. Add required number of random keys generated by the same method Random you already used and try to insert each value as a key. If the key is already in dictionary, simply generate another key. Make sure you do not repeat this process infinitely, which can happen when you already added all possible keys from the Random range you use.

That''s it.

—SA


您可以使用
You can use this[^] to create unique random numbers. It is a C# code but does what you need. Hope you can understand it.

Creating truly random numbers is a very tough job. If you need truly random numbers, search for any already existing library. I am not aware of one but google should be.


您可能可以编写自己的生成器或在线搜索以下示例:

1)
http://en.wikipedia.org/wiki/Normal_distribution [ http://en.wikipedia.org/wiki/Ziggurat_algorithm [ http://en.wikipedia.org/wiki/Box-Muller [
You could probably write your own generator or search online for examples of:

1)http://en.wikipedia.org/wiki/Normal_distribution[^]
2)http://en.wikipedia.org/wiki/Ziggurat_algorithm[^]
3)http://en.wikipedia.org/wiki/Box-Muller[^]

I had used Gaussian normal distribution long time back for a raffle draw system and it worked great for me.


这篇关于如何使真正的随机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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