![]() |
[vb6] need advice concerning a largescale non-recurring random number generator
i'm trying to come up with a pseudo random-number generator that will not generate duplicate numbers.
this number generator will generate a series of numbers. it can be 10 random numbers, or it could be well over 10000. to make a NG for the smaller end of the spectrum (10) is no big deal; i'd just run a for() loop to check newly generated values against those previously stored in an array. it's when i have to generate 1000+ numbers that i fear i'm going to have memory issues on older machines. i don't think a for() loop is going to cut it. can anyone give me some advice on how to go about making a memory-efficient, largescale random number generator? i think i read somewhere that you could use the current time to come up with continuously unique numbers or something. don't remember where i read it or what the details were. :( might this be relevant? thanks |
Why in the world would you need a prng that doesn't repeat any values?
|
because the generated numbers are going to be appended to filenames, where the number is going to be the only unique part of the filename.
File1092343.txt File3423498.txt File2342342.txt ... and so on. i can't have two "File1092343.txt" outputs. |
why do they have to be random? Why not just do
1.txt 2.txt 3.txt etc. |
That's the way they already are. The whole point of the program is to randomize them.
|
solution?
well, here's what i ended up doing.
i ended up having to limit the total number of generated numbers to 899, but i came up with (what i think is) a rather efficient way of generating 899 pseudo-random non-recurring numbers. first i generate a list of three-digit sequential numbers. (i started with 100 because i didn't feel like messing with the number formatting to create values like 001. the fact that i started with 100 is also the reason i can only generate 899 numbers, instead of 999.) :
100.txtthen i add a certain number of random digits to the front of the sequential numbers. since everything in the sequential number list is already a unique value, it doesn't matter if i make duplicate numbers with my random number routine. my output ends up looking like :
456100.txtand i can have values like :
884201.txtwithout breaking anything. if coded correctly, both of these operations can be performed within the same loop, thus eliminating the need to waste time comparing every single value to every single other value in the array. i'm sure this can be implemented on a larger scale like i envisioned, but it is beyond my capabilities to pull that off. |
As I see it u got 2 ways to solve this prb.
1. Use the high-precion-timer that follows with the OS, the multimedia timer.. to take the system clock( get next "tick"), which got alot of numbers something like 7836490734, and the next (tick) millisecond increments with 1, so you NEVER gonna get the number twice. If I remember it right it was a function called "GetNextTick()" in "mmci.dll". 2. I know there is a function in winapi that manages filenames for temporary filenames (I have NOT tested this). Here is a tool the made my breakthrough, API-Guide . This tools giving you way to look some simple examples for most of windows API's (I did use this tool in the days of windows 98, so I hope M$ havent f*** it up in XP :rolleyes: ) |
Quote:
thanks :) |
Oki here it is, this code DID work with Windows 98 and I haven't tested it with XP.
Put this in modul-section :
Option Explicit:
Dim fTime As Longbasically it's number of ticks since 1970 1.january, and 1 tick = 1 millisecond I hope this works for you, and remember this is VB6 code (not VB.net). :rolleyes: |
| All times are GMT -5. The time now is 8:00 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC