Click here to Skip to main content
16,020,669 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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 :

VB
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 ^_^
Posted
Updated 28-Apr-11 17:51pm
v2

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
 
Share this answer
 
Comments
Tarakeshwar Reddy 29-Apr-11 10:54am    
I may need to set a script to auto 5 you.
Sergey Alexandrovich Kryukov 29-Apr-11 11:04am    
Thank you very much, Tarakeshwar.
Do not do this mistake -- who will cover the situation is I make a mistake?
However, it would be interesting to see if you publish such script as a CodeProject article :-)
--SA
Tarakeshwar Reddy 29-Apr-11 12:06pm    
Actually I am thinking of writing my first article on the true random generator. I might have to sharpen my math skills.
Sergey Alexandrovich Kryukov 29-Apr-11 13:01pm    
Tarakeshwar,
This is a decent topic I'm not very well familiar with (I mean I know probability well, not those algorithms; I know they are quite problematic). So please do me a favor, notify me when you publish it. I would gladly read it.
--SA
Tarakeshwar Reddy 29-Apr-11 15:12pm    
Will do :)
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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Apr-11 1:00am    
I think situation is much more simple:
OP wants something different: just a list/array of random keys ***without repetition***. OP just failed to name it properly; of course it has nothing to do with "true" random.

Please see my solution.
--SA
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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Apr-11 1:00am    
Tarakeshwar,
This is very correct and good to know, but I think OP wants something different: just a list/array of random keys ***without repetition***. OP just failed to name it properly; of course it has nothing to do with "true" random.

Please see my solution.
--SA
Tarakeshwar Reddy 29-Apr-11 10:53am    
You are right, just using a collection and checking against it would solve his problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900