Link to home
Start Free TrialLog in
Avatar of dion_p1
dion_p1

asked on

Restict Random to 6 Characters

Im tryig to randomly generate password 6 Charachters...

Please help its giving me all sorts of size strings

For each strData In strInputData

Dim intPassword
Randomize
intPassword = Int(1234567 * Rnd() + 1)
     Set file = objFileSys.CreateTextFile(strExportFile & strCDate & " ExportWithPasswords.csv",TRUE)
     file.writeline(strData & "," & intPassword)
Next
file.close
Avatar of zorvek (Kevin Jones)
zorvek (Kevin Jones)
Flag of United States of America image

Try:

file.writeline(strData & "," & Format(Int(Rnd()*1000000),"000000"))

Kevin
Avatar of dion_p1
dion_p1

ASKER

this is vbs
Avatar of dion_p1

ASKER

I get these results

91666
13173
9935
36523
75810
66838
11205
47953
56761
98588
12475
18743
266
52253
76300
92728
21855
43363
77570
4158
2806

They all Need to be Six Charachters Long
Right...the Format function is not available in VBScript...sorrry about that.

Use this line:

file.writeline(strData & "," & Right("00000" & Int(Rnd()*1000000), 6)

You will get numbers like:

001243
198345
000001

Kevin
Here is a different way of looking at it...

randomize

msgbox randompass(6)
msgbox randompass(6)
msgbox randompass(6)

private function randomPass(length)
    chars = "1234567890" ' <-- this can be changed to include other chars if necessary
    for i = 1 to length
        randompass = randompass & mid(chars, Int(len(chars) * Rnd + 1), 1)
    next
end function
Avatar of dion_p1

ASKER

This is VBScript. I get Syntax Error at Privat Function......

Set oInputFile = objFileSys.OpenTextFile(strInputFile & strcDate & " ExportFromCases.csv")
strInputData = Split(oInputFile.ReadAll, vbNewline)

On Error Resume Next
For each strData In strInputData

      Set file = objFileSys.CreateTextFile(strExportFile & strCDate & " ExportWithPasswords.csv",TRUE)
      file.writeline(strData & "," & randompass(6))

private function randomPass(6)
    chars = "1234567890" ' <-- this can be changed to include other chars if necessary
    for i = 1 to length
        randompass = randompass & mid(chars, Int(len(chars) * Rnd + 1), 1)
    next
end function

Next
file.close
SOLUTION
Avatar of zorvek (Kevin Jones)
zorvek (Kevin Jones)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dion_p1

ASKER

Im using your Code Zorvek(Kevin) But still get some funny result...See Below

107375
783995
459640
753688
596094
832730
18758
210368
73953
105452
331694
128249
241
536794


All Random Numbers Must be 6 Charcters Long and No Shorter or Longer.

I also tried Idle Minds Solution and i Get an Error at Function Still.

Thanks In Advanced for you Help.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
1. Only Randomize once.  

2. Be aware of limitations of the VB PRNG:
http://www.15seconds.com/issue/051110.htm

3. Try to avoid or optimize concatenation functions:
Example:

private Function randomPass(parmLen As Long) As String
   Dim chars As String
   Dim OutBuffer As String
   Dim i as Long
   Dim charLen as Long
   chars = "0123456789ABCDEF" ' <-- these are Hex digits
   OutBuffer = Space(parmLen)
   charLen = Len(chars)

   For i = 1 to parmLen
        Mid$(OutBuffer, i, 1) = Mid$(chars, Int(charLen * Rnd + 1), 1)
   next

   randomPass = OutBuffer

end function

Note:  You can also use a byte array quite efficiently and get better performance than this string buffer example.
Not sure why your leading zeroes are lost. When I run this script in Explorer all the passwords have six digits:

<html>
<head>
</head>

<body><script type="text/vbscript">

Randomize
For Index = 1 to 100
   document.write(Right("00000" & Int(Rnd()*1000000), 6) & "<br />")
Next

</script></body>

</html>

Kevin
I've republished the VB PRNG article on EE:
http:A_11114.html