|
-
Aug 28th, 2007, 09:11 AM
#1
Unique random number generation?
So, the way I'm seeing this, there's two main methods. But first, the project scope... user enters lower and upper bounds, inclusive, and the number of values to be generated. Output is a read-only text box.
The first method, and currently in use:
A counter, starting at 0. A Do...Loop restricted to that counter. A random number is created within the bounds specified, and sent to a function, which loops through the current array. If the supplied value matches any value in the array, the function returns false, and the loop tries again. Don't knock me for it, I know it has limits. The closer you get to the max range, the longer it'll take... but it was quick and easy, and i was after 64 numbers from a list of 256. Not a big issue there.
The second method in my head is something as follows:
User enters the parameters. When user clicks Generate, a list is built of every legal value within the bounds. Using the counter mentality, probably a For...Next loop for this instance, the program would simply pick a random number between 0 and List.Count-1, and use that value, then promptly remove it. I can see this as being close to optimum, on the concept that it doesn't loop through someting constantly, and there'd be no risk of choosing duplicates, as a valid number would be removed from the list upon being chosen. My only concern is this: What if the user enters bounds that create a range of millions? That list would get enormous, and could possibly cause damage on an older computer.
What are your thoughts?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|