Consulting

Results 1 to 6 of 6

Thread: Unique Random Number Generation

  1. #1
    VBAX Tutor MINCUS1308's Avatar
    Joined
    Jun 2014
    Location
    UNDER MY DESK
    Posts
    254

    Unique Random Number Generation

    I'm making minesweeper in excel because they don't have any games on the computers at work.
    As part of this, I'm attempting to generate 'x' unique random numbers and then store them in MyArray.

         
    For i = 1 To NoOfMines
              Do
                   MineValue = CInt(Int((Size * Size - 1) * Rnd + 1))
              Loop Until IsError(Application.Match(CInt(MineValue), MyArray, 0))
              MyArray(i, 0) = MineValue
         Next I
    Typically I generate 'x-1' unique numbers with 1 number repeated. '[(x-1)+1=x]
    Every now and again I get 'x' unique numbers but its pretty rare.

    I cannot identify a pattern of repeats associated with the iteration of 'i'.
    Any thoughts about the folly in my logic?
    - I HAVE NO IDEA WHAT I'M DOING

  2. #2
    I would simply create a two-column table with all the mines in columnA and the rand function in B and then do a calc and subsequently sort on col B. Then simply take the number of items from the top.
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  3. #3
    VBAX Newbie
    Joined
    Jul 2018
    Posts
    1
    Location
    Hi Jan, can you explain how to do this exactly, please? For the less knowledgeable people like me. It looks like this is perfect for something I'm doing as well. Cheers.
    You can read this fascinating review of Rad-140 Testolone and learn more about it.

  4. #4
    Suppose you have employee names in column A (starting from A2) and you want to randomly draw 10 employees.
    In B2, enter =RAND()
    copy down to match the # of employees
    Select A and B and sort on column B. the top 10 employees are your draw. Want a new set of 10? simply sort on column B again.
    Regards,

    Jan Karel Pieterse
    Excel MVP jkp-ads.com

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    This might be a little simpler


    Option Explicit
    
    Sub MineSweeper()
    
        Const Size As Long = 10
        Const NumMines As Long = 20
    
        Dim MineField(1 To Size, 1 To Size) As Boolean
        Dim r As Long, c As Long, n As Long
        
        n = 0
        
        Randomize
        Do While n < NumMines
    
            r = Int(Size * Rnd + 1)
            c = Int(Size * Rnd + 1)
    
            If Not MineField(r, c) Then
                MineField(r, c) = True
                n = n + 1
            End If
        Loop
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  6. #6
    Store the result in an Array 'sn':
    Sub M_snb()
       [A1:A20] = "=rand()"
       sn = [index(rank(A1:A20,A1:A20),)]
       [A1:A20].clearcontents   
    End Sub
    Store the results in the worksheet
    Sub M_snb()
       [A1:A20] = "=rand()"
       [A1:A20] = [index(rank(A1:A20,A1:A20),)]
    End Sub

Posting Permissions

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