Click here to Skip to main content
16,020,261 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using a random function, but I want to void a same value from random.
Here my code :

VB
For i = 1 To PopuMain.members_count
           System.Windows.Forms.Application.DoEvents()
           ReDim PopuMain.Members(i).Genome(PopuMain.GenoLength)
           For j = 1 To PopuMain.GenoLength
               System.Windows.Forms.Application.DoEvents()
               PopuMain.Members(i).Genome(j) = Rand_between(1, Kromosom_Waktu)
               If j > 1 Then
                   For k = 1 To j - 1
                       If PopuMain.Members(i).Genome(j) = PopuMain.Members(i).Genome(k) Then
                           PopuMain.Members(i).Genome(j) = Rand_between(1, Kromosom_Waktu)
                       End If
                   Next
               End If
           Next j


And the random code is :

VB
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


What I want to reach is, each value(genome) from this rondom is different with other. Thank you ^_^
Posted
Updated 7-May-11 20:24pm

Do the Randomize outside the loop.
This will then prevent the (pretty high, depending on your processor speed) chance that the random numbers generated by Rnd() will be started from the same seed.

I would also suggest that you switch to a class level .NET Random Class[^] which would make your Rand_between routine un-necessary.

But nothing will guarantee that no two numbers in a sequence of random numbers is the same: that is the whole idea of random numbers - that the next number does not depend on the previous values. You are as likely to get a random number "x" followed by the same random number "x" as you are to get "x" followed by "x + 1": 1 in (number of values in the entire range). So if you generate numbers between 0 and 9 inclusive, the probability of getting any specific number are 1:10 each and every time.

If you need to ensure that you do not generate the same random number twice in any number space, then you will have to do a lot more work - and probably badly damage the randomness of the numbers you do get.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-May-11 2:44am    
Good points, my 5. One note: it cannot "badly damage the randomness", you can only deform the distribution of probability function, but you cannot add any correlation if original random number generation algorithm was more or less correlation-free -- think about it.

Now, as to the required algorithm -- I described it before; please see my answer.
--SA
My past answer to a very similar question should give you the idea:
How to make true random?[^].

—SA
 
Share this answer
 

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