Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic (http://www.programmingforums.org/forum18.html)
-   -   [vb6] need advice concerning a largescale non-recurring random number generator (http://www.programmingforums.org/showthread.php?t=10137)

chepfaust Jun 2nd, 2006 1:09 PM

[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

uman Jun 2nd, 2006 1:19 PM

Why in the world would you need a prng that doesn't repeat any values?

chepfaust Jun 2nd, 2006 1:39 PM

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.

uman Jun 2nd, 2006 5:07 PM

why do they have to be random? Why not just do
1.txt
2.txt
3.txt
etc.

chepfaust Jun 2nd, 2006 8:16 PM

That's the way they already are. The whole point of the program is to randomize them.

chepfaust Jun 7th, 2006 5:56 PM

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.txt
101.txt
...
349.txt
...
666.txt


then 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.txt
642101.txt
...
265349.txt
...
478666.txt


and i can have values like

:

884201.txt
884202.txt


without 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.

KTiger Jun 7th, 2006 7:51 PM

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: )

chepfaust Jun 7th, 2006 9:49 PM

Quote:

Originally Posted by KTiger
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".

argh! that's exactly what i was trying to figure out days ago but nobody answered.

thanks :)

KTiger Jun 8th, 2006 6:04 AM

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

'Time Device
Public Declare Function GetTickCount& Lib "kernel32" ()

and put this in either form or module
:

Dim fTime As Long
fTime = GetTickCount&

And just repeat GetTickCount to get new tick, ie: 7893244643
basically 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