Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi I made password generator

VB
Public Function RndWord(ByVal WordLength As Integer) As String
       Dim TempWord As String
       Dim loopvar As Integer
       For loopvar = 1 To WordLength
           TempWord = TempWord & Mid("abcdefgABCDEFG", Int(Rnd() * 14) + 1, 1)
       Next loopvar
       RndWord = TempWord
   End Function


and every time I use first use it generates the same first 3 passwords every time! how can I fix this???
Posted

Before using Rnd(), use Randomize().


In VB, Rnd() uses a maths equation to produce the next 'random' number.

But, because the actual operation is known you can predict the next value.

However given an arbitray start value the numbers have good distribution - these are 'pseudo-random' numbers.

To keep Rnd() from startng at the same number (and hence giving the same sequence of numbers every time), Randomize() should be called to use the machine clock to set the initial value (called a seed).

In the .Net use System.Random instead if you can.
 
Share this answer
 
v2
What about documentation [^]:

For any given initial seed, the same number sequence is generated because each successive call to the Rnd function uses the previous number as a seed for the next number in the sequence.
Before calling Rnd, use the Randomize statement without an argument to initialize the random-number generator with a seed based on the system timer.


?
 
Share this answer
 
Why are you calling Rnd() in a .NET class ? Why not use the .NET methods ? I have no idea how Rnd() is seeded, but your issue is that the seed is the same. There's no such thing as random numbers on a PC.
 
Share this answer
 
Comments
Code Master38 19-Jun-10 0:54am    
Reason for my vote of 2
you don't know what your talking about

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