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 = fs
penTextFile("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
Then
x = x - 1
End If
Next
Next
Set textfile = fs
penTextFile("c:\results.txt", ForAppending, True)
For z = 0 to 4
textfile.WriteLine(randomnumber(z))
Next
textfile.Close
-----------------------End of Code-------------------
Thanks.
-----------------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 = fs
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
x = x - 1
End If
Next
Next
Set textfile = fs
For z = 0 to 4
textfile.WriteLine(randomnumber(z))
Next
textfile.Close
-----------------------End of Code-------------------
Thanks.