![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Feb 2005
Posts: 62
Rep Power: 4
![]() |
[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 |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 5
![]() |
Why in the world would you need a prng that doesn't repeat any values?
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials. --WilliamSChips on Slashdot |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Feb 2005
Posts: 62
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#4 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 5
![]() |
why do they have to be random? Why not just do
1.txt 2.txt 3.txt etc.
__________________
Few people deserve to be compared to (Rush) Limbaugh, most of them were convicted at the Nuremburg trials. --WilliamSChips on Slashdot |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Feb 2005
Posts: 62
Rep Power: 4
![]() |
That's the way they already are. The whole point of the program is to randomize them.
|
|
|
|
|
|
#6 |
|
Programmer
Join Date: Feb 2005
Posts: 62
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#7 |
|
Newbie
Join Date: May 2006
Location: Narvik, Norway
Posts: 22
Rep Power: 0
![]() |
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 )
__________________
To sell your own books it's like selling a piece of your soul. Kim |
|
|
|
|
|
#8 | |
|
Programmer
Join Date: Feb 2005
Posts: 62
Rep Power: 4
![]() |
Quote:
thanks ![]() |
|
|
|
|
|
|
#9 |
|
Newbie
Join Date: May 2006
Location: Narvik, Norway
Posts: 22
Rep Power: 0
![]() |
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" () Dim fTime As Long fTime = GetTickCount& 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). ![]()
__________________
To sell your own books it's like selling a piece of your soul. Kim |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|