Solved

Restict Random to 6 Characters

Posted on 2006-11-15
12
196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 86

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 86

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

730 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