Solved

Restict Random to 6 Characters

Posted on 2006-11-15
12
189 Views
Last Modified: 2013-01-26
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
0
Comment
Question by:dion_p1
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 17952740
Try:

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

Kevin
0
 

Author Comment

by:dion_p1
ID: 17952885
this is vbs
0
 

Author Comment

by:dion_p1
ID: 17952908
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
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 17953165
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
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17953555
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
0
 

Author Comment

by:dion_p1
ID: 17953725
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
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 81

Assisted Solution

by:zorvek (Kevin Jones)
zorvek (Kevin Jones) earned 250 total points
ID: 17953803
Since IdleMind has not responded I'll give it another crack.

This code will give you what you originally asked for:

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 & "," & Right("00000" & Int(Rnd()*1000000), 6)

Next
file.close

IdleMind's solution will give you the exact same result but with the added flexability of using different sets of characters for the password. To implement IdleMind's solution you need to include the function before your main variable declarations and code:

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

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))

Next
file.close

Kevin
0
 

Author Comment

by:dion_p1
ID: 17954231
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.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 17956697
The function should be at the bottom of the code and it should have "length" inbetween the parenthesis as I originally had:

randomize
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))
Next
file.close

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
0
 
LVL 45

Expert Comment

by:aikimark
ID: 17958244
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.
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 17959336
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
0
 
LVL 45

Expert Comment

by:aikimark
ID: 38822734
I've republished the VB PRNG article on EE:
http:A_11114.html
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now