Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Number Generator

Status
Not open for further replies.

bkesting

IS-IT--Management
Apr 14, 2003
180
US
Hello, I was wondering if anyone could help me out with a script I am writing. I am trying to write a script that will generate 5 random numbers in the range of 1 to 500. Once the numbers have been chosen, they should be removed from the list so that they are not chosen again. I have tried to come up with a routine, but I am still getting duplicates. Maybe someone else with a different perspective can see where I am going wrong. Right now, I generate the 5 numbers and write them to a text file. Then the next time I run the program, the script checks the newly generated numbers against those already present in the txt file.

-----------------Start of Code--------------
dim fso, textfile
redim readnumbers(0)
dim randomnumber(4)
Const ForReading = 1, ForWriting = 2, ForAppending = 8

i = 0
upper = 500
lower = 1

Set fso = CreateObject("Scripting.FileSystemObject")
Set textfile = fso_OpenTextFile("c:\results.txt", ForReading, True)

While not textfile.AtEndOfStream
redim Preserve readnumbers(i)
readnumbers(i) = textfile.ReadLine
i = i + 1
Wend
textfile.Close

For x = 0 to 4
randomize timer
randomnumber(x) = Int((upper - lower + 1) * Rnd) + lower
For y = 0 to i - 1
If randomnumber(x) = readnumbers(y) Then
x = x - 1
End If
Next
Next


Set textfile = fso_OpenTextFile("c:\results.txt", ForAppending, True)

For z = 0 to 4
textfile.WriteLine(randomnumber(z))
Next

textfile.Close
-----------------------End of Code-------------------


Thanks.
 
To explain the code a little.....

The While/Wend loop basically counts the number of lines in the txt file and sets up the array so the script knows how many numbers are in the txt file that it needs to check for.

The For/Next X loop generates the 5 random numbers and the For/Next Y loop checks each number against the numbers read from the txt file.

 
just an aside but what happens on the 101st time you try to run this and ALL of the numbers have been taken?
 
I will work out the bugs of when all the numbers expire later, that will be easy.

No this is not a homework project, this is for a building inspection program that selects rentral properties at random to be inspected to see that they are in compliance with local building code. Each number represents a property in a master database.

My problem is I just am not familiar enough with vbscript to finish this off. I have learned alot just be browsing google. Perhaps I just need to give my mind a day's break or so to get a fresh persepective on this.

 
Each time you use the script to generate the random numbers, you'll get an entirely different set, though some may be in the first 5 at some time or another. If you want to have all 500 properties inspected at random over 8 years, then store the numbers in a file and then select the first 8 and delete those from the file.

There are a couple ways that I've used to avoid duplicate numbers in the set, and this is one of the quicker ones for checking for duplicates:

Code:
'This creates the numbers and saves them to a string
'with each number separated by a pipe: |
'If the number generated is NOT in the string
'with a pipe on either side, then add it to the string
'and put the pipe at the end

Dim max, randnumbers, randstring, onenumber, randcount
randstring = "|"
Randomize Timer
randcount = 0
max = 500

Do
  onenumber = CStr(Int(500 * Rnd) + 1)
  If InStr(randstring, "|" & onenumber & "|") = 0 Then
    randstring = randstring & onenumber & "|"
    randcount = randcount + 1
  End If
Loop Until randcount = max

'Trim off the first and last pipes before splitting
randstring = Mid(randstring, 1)
randstring = Left(randstring, Len(randstring) - 1)
randnumbers = Split(randstring, "|")

'then write the array to the file

'Each time period read the whole file,
'retrieve the first 8 numbers,
'then rewrite the remaining ones back to the file

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top