|
-
May 15th, 2001, 01:59 AM
#1
Thread Starter
Lively Member
set of random ints
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.
-
May 15th, 2001, 07:27 AM
#2
transcendental analytic
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.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
May 16th, 2001, 02:05 AM
#3
Thread Starter
Lively Member
?
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
-
May 16th, 2001, 09:56 AM
#4
Banned
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.
-
May 18th, 2001, 11:42 AM
#5
How about:
Code:
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
-
May 21st, 2001, 06:10 AM
#6
Thread Starter
Lively Member
never mind
i got it after a few brainswarming sessions with the bees in my backyard!
its a mere 10 line code and workd just fine.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|