Re: How to loop through randomizer without getting the same digits.



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





.



Relevant Pages


Loading