Results 1 to 6 of 6

Thread: set of random ints

  1. #1

    Thread Starter
    Lively Member moonguy's Avatar
    Join Date
    Apr 2001
    Location
    pune(india)
    Posts
    67

    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.
    Vishal Mungi
    [email protected]
    www.geocities.com/vishalmungi

    VB,ASP,C++,Java,Oracle,MAX,photoshop,XML..,


    ~~~~
    \ - - /

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  3. #3

    Thread Starter
    Lively Member moonguy's Avatar
    Join Date
    Apr 2001
    Location
    pune(india)
    Posts
    67

    ?

    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
    Vishal Mungi
    [email protected]
    www.geocities.com/vishalmungi

    VB,ASP,C++,Java,Oracle,MAX,photoshop,XML..,


    ~~~~
    \ - - /

  4. #4
    Banned
    Join Date
    Feb 2001
    Location
    Back to sh*tland
    Posts
    294
    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.

  5. #5
    jim mcnamara
    Guest
    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

  6. #6

    Thread Starter
    Lively Member moonguy's Avatar
    Join Date
    Apr 2001
    Location
    pune(india)
    Posts
    67

    Cool 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.
    Vishal Mungi
    [email protected]
    www.geocities.com/vishalmungi

    VB,ASP,C++,Java,Oracle,MAX,photoshop,XML..,


    ~~~~
    \ - - /

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width