Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Number Array - Each Value Once 3

Status
Not open for further replies.

mwolf00

Programmer
Nov 5, 2001
4,177
0
0
US
I am trying to create an array of random numbers - with values between 0 and 188. I want the numbers ordered randomly, but I want to make sure that every number is used once. Here's what I have, which loops endlessly trying to land on the last exact numbers...
Code:
    Dim i As Integer
    Dim j As Integer
    Dim randomNumber As Integer
    Randomize
    For i = 0 To 188
        numArr(i) = -1
    Next
    
    i = 0
    Do While i <= 188
        randomNumber = Round(188 * Rnd(1), 0)
       
         For j = 1 To UBound(numArr)
             If randomNumber = numArr(j) Then
                 Exit For
             ElseIf numArr(j) = -1 Then
                 numArr(j) = randomNumber
                 i = i + 1
                 Exit For
             End If
         Next
    Loop

Thanks for any help!
-- Just trying to help... LOL [ponder]
 
1) No.
2) Collections are predefined in thier size/dimensions. Array(x,y,z). To achieve this in a collection you have to add collections,(or arrays). Also if you want to work with a deeper collection then you dim a new collection and set it to the collection, for example;

dim c1 as new collection
dim c2 as new collection
dim c3 as collection

c1.add c2
set c3 = c1.item(0)
c3.add ( something )
set c3 = nothing

also you can only redim an array once, collections are dynamic which makes them more flexible.

3) Other wise it would also return a zero, in this case the lowest returned number is one.

4) It should, though I haven't tried objects like images.

5) You can mix the collection items as you please, but don't forget you need to retrieve things accordingly. If you add a number then retrieve it with the same or larger variable, and so on.
 

merlinx,

also you can only redim an array once

not true

and

update : 2) arrays are predefined

this is also not true in this context


 
Thanks to all for all of the help (esp - merlinx). I'm going to incorporate collections into my code often - It looks like i'm going to like it!

Kieren, I haven't worked with dictionaries either, but I think that I'll stick to learning collections first....

Programming....a million and one ways to add two plus two.... -- Just trying to help...
[wolf]<--- This is a wolf? We need a new icon.......
mikewolf@tst-us.com
 
vb5,

I'll read back up on that, that is what I learned. point 2 refers to the first point.
 
vb5,

Your correct, MSDN say's you can redim an array as often as you like. The dimensions but not the type.
 

Case in point. Try this code...

[tt]
Option Explicit

Private Sub Form_Load()

Dim MyArray(), I As Integer

ReDim MyArray(3)

ReDim MyArray(7)

For I = LBound(MyArray) To UBound(MyArray)
MyArray(I) = I
Next I

ReDim Preserve MyArray(8)

MyArray(8) = 8

Erase MyArray

ReDim MyArray(11)

For I = LBound(MyArray) To UBound(MyArray)
MyArray(I) = I
Next I

For I = LBound(MyArray) To UBound(MyArray)
Debug.Print MyArray(I)
Next I



End Sub
[/tt]

Good Luck
 
also you can only redim an array once

is not correct. You can redim an array as many times as necessary. What you cannot do is change the number of dimensions or the size of any dimension except the last on via redim.

While I have used the For Each loop control construct, I have seen refereences to dropping the For - Next construct in favor of when and do constructs. so I'm attempting to be somewhat consistient in the control logic used in programming (and Yes, I am aware that &quot; ... consistency is the hobgobblin of little minds ... &quot;) - bit it also makes program maintenance a bit easier.

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Just an FYI about dictionarys...Dictionarys are very similar to collections, are easier to use, and more efficient (faster). They support using For/Each syntax as well. The most cool thing about them though is that they have the .Exists method which is very useful for a wide number of things (like this problem of ensuring uniqueness).
 
ok kieren, I'll ask....

How does your code ensure that the random number generator doesn't keep chosing the wrong value when you only have one or two numbers left to add to your dictionary? The whole discusion started when my code maxed out the processor while serching for those last few random numbers..... -- Just trying to help...
[wolf]<--- This is a wolf? We need a new icon.......
mikewolf@tst-us.com
 
It doesn't make any attempt to...It really isn't necessary, because the code is so fast. In fact, in the modified code below, I am getting the first 10,000 integers in random order (without repeats)...It only takes about 0.25 seconds to run on my machine (which is not particularly fast). To get the first 10,000 numbers it gennerally loops about 85,000 times. I suspect if you went up into the millions, you would have to find a more efficient approach.

Private Sub form_load()
Randomize
Dim curTime As Double
curTime = Timer
Dim RandArray(9999) As Long
Dim RandomNumber As Long
Dim RandDict As Dictionary 'set Micrsoft Scripting Runtime reference
Set RandDict = New Dictionary
Dim i As Long
Dim Tries As Double
Do Until i = 9999
RandomNumber = Round(10000 * Rnd(1), 0)
If RandDict.Exists(RandomNumber) = False Then
RandDict.Add RandomNumber, 0
RandArray(i) = RandomNumber
'Debug.Print RandArray(i)
i = i + 1
End If
Tries = Tries + 1
Loop
Debug.Print &quot;It took: &quot; & Timer - curTime & &quot; seconds. (&quot; & Tries & &quot; tries to get all the numbers)&quot;
End Sub
 
I see, my code wasn't bogging down generating the number (I already knew that) - It was taking too long to check the number against the whole array (one value at a time). The EXISTS method of the dictionary object is exponentially faster - I like it too, another star!- -- Just trying to help...
[wolf]<--- This is a wolf? We need a new icon.......
mikewolf@tst-us.com
 
Hmmmmmmmm ... mmmmmmmm,

Just for the thrill of the chase, I did a similar transform of kieren's dictionary. I, also, have not used the dict very much and wanted to see the claim of increased performance of this object. Realizing that I'm more or less an amatur in the comparision timing business, I'll try to not publish the trivial code, but just mention that both routines were &quot;fed&quot; the same size of array and the timings were taken the same way -via code.

After all of this several minutes of opreperation, I do not see any significant difference in these approaches -at least on the ancient and crumbling system (450 MHz): but enough of the &quot;whine and complin&quot; option - and on to the results in the first three races at &quot;Balto No Job&quot;, the spirial track to oblivion:

Code:
#Recs            10K     25K     50K
Race # 1(Dict)    4S     39S    219S
Race # 2(Aray)    4S     41S    215S

While I'm sure some will cling to the differences shown here, I'm MUCH more concerned with the aparent difference between the overall performance of &quot;My&quot; system and kieren system. This is aproaching two orders of magnitude (4 / 0.25) = 16) and kieren is STILL able to lament that her system is &quot; ... (which is not particularly fast) ... &quot; - so that places me aproximatly dead last in the slow lane of pre-history.

Oh, my, ... I DO need some charity? Don't I?

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
I guess what I was trying to say is that Dictionary objects are signifigantly faster to use than collections...as you found out, they aren't any faster than an array, though they might be easier to use...There is an interesting thread on this at:

thread222-220514
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top