Solved

Restict Random to 6 Characters

Posted on 2006-11-15
12
193 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

803 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