Solved

Restict Random to 6 Characters

Posted on 2006-11-15
12
185 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 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