Re: How to loop through randomizer without getting the same digits
- From: Mike A <MikeA@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 15 Feb 2006 08:33:10 -0800
I moved the randomize to the top, right after I dim the variables, and after
the first pass through the loop ( generate the random digits, and then
compare it to sitecodes in sms db) when it goes to the subroutine to
regenerate the digits again, it gets the generates the same thing as the
first pass.
Here's the output I get ( I cut out some lines, because we have 2900 sites.
Thats why you'll see the counter # jump from 3 to 291, etc...). I made it
comment so I could see where it is. The first pass it generates sitecode
F1Q, then it compares this to all the sitecodes listed in SMS. It finds a
match so it toggles the unique value to no, denoted by the Unique? 7.
Then it goes back to the until unique = Yes loop and should regenerate, but
instead it generates F1Q again.
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
2/15/2006 11:20:05 AM - Connected Successfully
2/15/2006 11:20:06 AM - Number of sites is: 2905
1 :Comparing A01 to F1Q, Unique? 6 1
2 :Comparing A02 to F1Q, Unique? 6 1
3 :Comparing A1F to F1Q, Unique? 6 1
291 :Comparing F1M to F1Q, Unique? 6 1
292 :Comparing F1O to F1Q, Unique? 6 1
293 :Comparing F1P to F1Q, Unique? 6 1
2903 :Comparing W24 to F1Q, Unique? 7 1
2904 :Comparing W25 to F1Q, Unique? 7 1
2905 :Comparing W26 to F1Q, Unique? 7 1
The sitecode F1Q is already in use, Generating a new one.
2/15/2006 11:20:33 AM - Connected Successfully
2/15/2006 11:20:33 AM - Number of sites is: 2905
1 :Comparing A01 to F1Q, Unique? 6 2
2 :Comparing A02 to F1Q, Unique? 6 2
3 :Comparing A1F to F1Q, Unique? 6 2
4 :Comparing A1G to F1Q, Unique? 6 2
5 :Comparing A1I to F1Q, Unique? 6 2
289 :Comparing F1K to F1Q, Unique? 6 2
293 :Comparing F1P to F1Q, Unique? 6 2
294 :Comparing F1Q to F1Q, Unique? 7 2
295 :Comparing F1R to F1Q, Unique? 7 2
296 :Comparing F1S to F1Q, Unique? 7 2
297 :Comparing F1T to F1Q, Unique? 7 2
298 :Comparing F1U to F1Q, Unique? 7 2
299 :Comparing F1V to F1Q, Unique? 7 2
300 :Comparing F1W to F1Q, Unique? 7 2
"Richard Mueller" wrote:
Hi,.
You should only call Randomize at the beginning of your program, then never
again. From then on each call to Rnd generates a new random number.
--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
"Mike A" <MikeA@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:BADC77EB-6EC4-4979-A2E9-AD3A8B50BC38@xxxxxxxxxxxxxxxx
Hopefully there are some Vbscript Gurus who who can help me. I am
currently
managing 2800 SMS Sites, one Central, 26 Primary and about 2770
secondaries.
We regularly have new branches open and we are required to create new
secondary sites and need new sitecodes. So I am writing a vbscript to do
the
following steps.
1. Prompt for the 2 digit state code ( NC, SC, GA) and then make the first
digit of the sitecode the First digit of the state, like NC is N, etc.
2. Generate 2 random digits (numeric or Alpha) for the second and third
digits of the sitecode.
3. Query the sites table in the SMS Database to make sure the sitecode
isn't
currently in use.
If so, loop back to the top and start the process over at the 2 random
digits.
Everything works perfectly, except for when I run into a sitecode that
isn't
unique. I use randomize and the rnd function, it always generates the
same
random digits. I think its because I am basing the seed number off the
system timer, but even when I passed randomize a number that incremented,
it
still generated the same random digits after running through the first
time.
Here is my script. Any ideas on what I might be doing wrong with the
randomize or rnd?
Thanks,
Dim MyValue, Response, Response2
Dim intupperlimit,intlowerlimit, oSiteState,
ofirstdigit,oseconddigit,othirddigit,MyValue2
Dim conn, rs, strSQLQuery, adOpenStatic, intSMSSiteNumber, ExistSC
Dim SiteCounter
Dim SiteArr(5000)
Dim Arrcounter
Dim oNewSitecode
Dim Unique, RandCounter
intupperlimit = 90
intlowerlimit = 48
intupperlimit2 = 90
intlowerlimit2 = 48
oSiteState = InputBox ("Enter the State the new site is for: ","State")
Select Case (UCase(oSiteState))
Case "FL" ofirstdigit = "F"
Case "GA" ofirstdigit = "G"
Case "NC" ofirstdigit = "N"
Case "VA" ofirstdigit = "V"
Case "NJ" ofirstdigit = "N"
Case "NY" ofirstdigit = "N"
Case "DC" ofirstdigit = "D"
Case "DE" ofirstdigit = "D"
Case "MD" ofirstdigit = "M"
Case "SC" ofirstdigit = "S"
Case "PA" ofirstdigit = "P"
Case "AL" ofirstdigit = "A"
Case "TX" ofirstdigit = "T"
Case "CA" ofirstdigit = "C"
Case Else ofirstdigit = ""
End Select
If ofirstdigit <> "" Then
Unique = vbNo
RandCounter = 0
Do Until Unique = vbYes
'Now its time to get the second digit
RandCounter = RandCounter + 1
'oNewSitecode = Null
'MyValue = Null
'MyValue = Null
Call random1
Call random2
oNewSitecode = ofirstdigit & oseconddigit & othirddigit
Call UniqueSC
If Unique = vbYes Then
WScript.Echo "The Unique sitecode is: " & oNewSitecode
Else
WScript.Echo "The sitecode " & oNewSitecode & " is already in use,
Generating a new one."
End If
loop
Else
WScript.Echo "You didn't enter a valid 2 digit state code. Script
exiting."
End If
'Begin Subroutines
Sub random1
Randomize ' Initialize random-number generator.
Do Until Response = vbNo
MyValue = Int((intupperlimit * Rnd) + intlowerlimit)
If MyValue < intlowerlimit Then
Response = vbYes
Else If MyValue > intupperlimit Then
Response = vbYes
Else
Select Case (MyValue)
Case "58" Response = vbYes
Case "59" Response = vbYes
Case "60" Response = vbYes
Case "61" Response = vbYes
Case "62" Response = vbYes
Case "63" Response = vbYes
Case "64" Response = vbYes
Case Else Response = vbNo
End Select
End If
End if
oseconddigit = UCase(Chr(MyValue))
'WScript.Echo "The second digit is: " & oseconddigit & " "& MyValue
'Response = MsgBox ("Roll again? ", vbYesNo)
Loop
End Sub
Sub random2
Randomize(RandCounter) ' Initialize random-number generator.
Do Until Response2 = vbNo
MyValue2 = Int((intupperlimit2 * Rnd) + intlowerlimit2)
If MyValue2 < intlowerlimit2 Then
Response2 = vbYes
Else If MyValue2 > intupperlimit2 Then
Response2 = vbYes
Else
Select Case (MyValue2)
Case "58" Response2 = vbYes
Case "59" Response2 = vbYes
Case "60" Response2 = vbYes
Case "61" Response2 = vbYes
Case "62" Response2 = vbYes
Case "63" Response2 = vbYes
Case "64" Response2 = vbYes
Case Else Response2 = vbNo
End Select
End If
End if
othirddigit = UCase(Chr(MyValue2))
'WScript.Echo "The Third digit is: " & othirddigit & " "& MyValue2
'Response = MsgBox ("Roll again? ", vbYesNo)
Loop
End Sub
Sub UniqueSC
set conn=CreateObject("ADODB.Connection")
conn.Open "DRIVER={SQL Server};SERVER=CentralServer;DATABASE=SMS_N01;"
if err.number<>0 then
'wscript.echo now() & " - Error " & err.number & ", " & err.Description
Wscript.Echo now() & " - Error " & err.number & ", " & err.Description
wscript.quit(1)
Else
'WScript.Echo Now() & " - Connected Successfully"
Wscript.Echo Now() & " - Connected Successfully"
end If
set rs = CreateObject("ADODB.RecordSet")
Set rs.ActiveConnection = conn
adOpenStatic = 3
strSQLQuery = "select SiteCode from Sites"
rs.Open strSQLQuery, conn, adOpenStatic
if err.number<>0 Then
'wscript.echo now() & " - Error " & err.number & ", " & err.Description
Wscript.Echo now() & " - Error " & err.number & ", " & err.Description
wscript.quit(1)
end If
intSMSSiteNumber = rs.RecordCount
if err.number<>0 Then
'wscript.echo now() & " - Error " & err.number & ", " & err.Description
Wscript.Echo now() & " - Error " & err.number & ", " & err.Description
'wscript.quit(1)
end if
'wscript.echo now() & " - Collection members: " & intCollectionMembers
Wscript.Echo now() & " - Number of sites is: " & intSMSSiteNumber
Sitecounter = 0
Unique = vbYes
Do Until rs.EOF
ExistSC = rs("Sitecode")
'WScript.Echo strName
'Wscript.Echo strName
'SiteArr(SiteCounter) = ExistSC
If ExistSC = oNewSitecode Then
Unique = vbNo
End If
SiteCounter = SiteCounter + 1
WScript.Echo SiteCounter & " :Comparing " & ExistSC & " to " &
oNewSitecode
& ", Unique? " & Unique & " " & RandCounter
rs.MoveNext
Loop
'Unique = vbYes
'Arrcounter = 0
'Do Until Arrcounter = intSMSSiteNumber + 1
'WScript.Echo "Comparing Array Value " & SiteArr(Arrcounter) & " with our
Random Sitecode " & oNewSitecode
'If SiteArr(Arrcounter) = oNewSitecode Then
' Unique = vbNo
'End If
'Arrcounter = Arrcounter + 1
'Loop
End Sub
- References:
- How to loop through randomizer without getting the same digits.
- From: Mike A
- Re: How to loop through randomizer without getting the same digits.
- From: Richard Mueller
- How to loop through randomizer without getting the same digits.
- Prev by Date: Re: VBS runs, VBE doesn't
- Next by Date: Getting filenames (selected by date) into a text file
- Previous by thread: Re: How to loop through randomizer without getting the same digits.
- Next by thread: CDO.Message seems to remove formatting fro Excel file
- Index(es):
Relevant Pages
|