Click to See Complete Forum and Search --> : set of random ints
moonguy
May 15th, 2001, 01:59 AM
hi there ,
i need to have a set of fully random integers in a session i.e any of the old one's shoul'nt be repeated in that session.I've tried with numerous nested for loops combined with if statements in vb but no success.I thought some of u guys/gals can provide me an answer.
thanks in advance.
kedaman
May 15th, 2001, 07:27 AM
you only need two nested loops, the outher for assigning new random values, inner for searching trough already assigned values for a match with a test random value. If no match is found, assign the value and increment index.
moonguy
May 16th, 2001, 02:05 AM
so what am i doing wrong here:
Dim arr(9) As Integer
Dim i As Integer
Dim j As Integer
Dim nxt As Integer
Private Sub Form_Load()
i = 0
Randomize
Do While i < 10
For j = 0 To i
nxt = Rnd * 10 \ 1
If nxt = arr(j) Then
Else: arr(i) = nxt
End If
i = i + 1
Next
Loop
End Sub
Good Dreams
May 16th, 2001, 09:56 AM
If nxt = arr(j) Then (you need to put something here)
Else
arr(i) = nxt
Since i=0, it will never execute the code in the For .. Next block.
jim mcnamara
May 18th, 2001, 11:42 AM
How about:
dim range as Integer ' maximum possible integer value
dim arr() as Integer
dim howmany as Integer ' the number of integers you need
range = 500
howmany = 100
Gen arr(), howmany, range ' get 100 non repeating random #
' in the range 1 - 500
.......
Sub Gen(arr() as Integer, many as Integer, limit as integer)
dim j as integer, i as Integer
i = many
Redim arr(1 to i)
For j = 1 to limit: arr(j) = j: next j
Do while i > 1
swap arr(i), arr(int(rnd*limit+1))
i = i - 1
Loop
End Sub
Sub Swap(i as integer, j as integer)
dim tmp as Integer
tmp = i
i=j
j=tmp
End Sub
moonguy
May 21st, 2001, 06:10 AM
i got it after a few brainswarming sessions with the bees in my backyard!
its a mere 10 line code and workd just fine.
:p
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.