locked
Random number Generator between 1 and 4

    Question

  • I need to create a multi dimensional array comprised of random numbers between 1 and 4. The reason why I need a mutidimensional array is because each number needs to be associated with a random letter of F or N. [ F or N are really Indicators or whether I should show the user Feedback on their results in the case of F. Or not show them Feedback in the case of N.]  So within this array 2 random numbers will be associated with random F's. And 2 will be associated with  random N's. For example the array should look like (1,F 2,F 3,N  4,N) or (3,N 2,F 1,N 4,F). The reason why I need this random array is because during the course of my application being ran 4 times (which represents the numbers)  I want to insure that every time N is done 2 times and F is done 2 times. This is all to make sure that each Number 1-4  after 100 times the application is ran has the same amount of F's and N's. I was also wondering if someone knows a way to store how many times the application has been ran in asp.net so that I can recreate another random string after 4 application runs. Thanks for any advice.
    Sunday, November 21, 2010 4:08 PM

Answers

  • Try this one

        Dim PRNG As New Random

        Private Sub Button1_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) Handles Button1.Click

            Dim fn() As String = New String() {"F", "N"}
            Dim tmpRslts As New List(Of String)
            Dim currentSel As String

            For numQ As Integer = 1 To 100
                Dim ctf As Integer = 0
                Dim ctn As Integer = 0
                Dim numP As New List(Of String)
                For x As Integer = 1 To 4
                    numP.Add(x.ToString)
                Next
                'do groups of four
                'each group of four will have two f's and two n's
                For x As Integer = 1 To 4
                    Dim rNumX As Integer = PRNG.Next(0, numP.Count)
                    Dim rNum As String = numP(rNumX)
                    numP.RemoveAt(rNumX)
                    If ctf < 2 AndAlso ctn < 2 Then
                        'could be F or N
                        Dim ForN As Integer = PRNG.Next(0, 2) 'select either
                        currentSel = fn(ForN)
                    ElseIf ctf < 2 Then
                        'must be F
                        currentSel = fn(0)
                    Else
                        'must be N
                        currentSel = fn(1)
                    End If
                    tmpRslts.Add(rNum & "," & currentSel) 'add to temp results
                    'do counts
                    If currentSel = fn(0) Then
                        ctf += 1 'count f
                    Else
                        ctn += 1 'count n
                    End If
                Next
                Stop 'use the debugger to verify that each group of four has two f's and two n's
            Next

            'randomize ;) the results
            Dim theResults As New List(Of String)
            For x As Integer = 0 To tmpRslts.Count - 1
                Dim idx As Integer = PRNG.Next(0, tmpRslts.Count)
                theResults.Add(tmpRslts(idx))
                tmpRslts.RemoveAt(idx)
            Next

            Stop 'use the debugger to verify theResults appear random

        End Sub


    Subnet Calculator / Planner      Serial Port      Random
    • Marked as answer by Jack_Oh Sunday, November 21, 2010 5:44 PM
    Sunday, November 21, 2010 5:42 PM
  • Or

     

    Public Class Form1
        Dim PRNG As New Random

        Private Sub Button1_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) Handles Button1.Click

            Dim fn() As String = New String() {"F", "N"}
            Dim nums() As String = New String() {"1", "2", "3", "4"}
            Dim tmpRslts As New List(Of String)

            For numQ As Integer = 1 To 100
                Dim fnL As New List(Of String)
                Dim fnIdx As Integer
                Dim numsL As New List(Of String)
                Dim numsIDX As Integer
                fnL.AddRange(fn) 'add enough F/N
                fnL.AddRange(fn)
                numsL.AddRange(nums) 'add numbers
                Do While numsL.Count > 0
                    fnIdx = PRNG.Next(0, fnL.Count) 'get an FN
                    numsIDX = PRNG.Next(0, numsL.Count) 'get a number
                    tmpRslts.Add(numsL(numsIDX) & "," & fnL(fnIdx)) 'save the results
                    numsL.RemoveAt(numsIDX) 'remove the items just used
                    fnL.RemoveAt(fnIdx)
                Loop

                Stop 'use the debugger to verify that each group of four has two f's and two n's
            Next

            'randomize ;) the results
            Dim theResults As New List(Of String)
            For x As Integer = 0 To tmpRslts.Count - 1
                Dim idx As Integer = PRNG.Next(0, tmpRslts.Count)
                theResults.Add(tmpRslts(idx))
                tmpRslts.RemoveAt(idx)
            Next

            Stop 'use the debugger to verify theResults appear random

        End Sub
    End Class

     

    I like this better than the one picked as the answer.


    Subnet Calculator / Planner       Serial Port       Random
    • Marked as answer by Jack_Oh Monday, November 22, 2010 12:28 AM
    Sunday, November 21, 2010 6:01 PM
  • I think the easiest way to generate a random sequence of those four digit numbers is to just pick an entry from the list of all possible values.  Take a look at the following to see what I mean:
     

     Sub Main()
      Dim sets As New List(Of Integer)( _
       {1234, 1243, 1324, 1342, 1423, 1432, _
        2134, 2143, 2314, 2341, 2413, 2431, _
        3124, 3142, 3214, 3241, 3412, 3421, _
        4123, 4132, 4213, 4231, 4312, 4321 })
      Dim r As New Random()
     
      For item As Integer = 1 To 25
       Console.WriteLine(sets(r.Next(0, sets.Count)))
      Next
     
      Console.ReadKey()
     End Sub
     
     

     

    --
    Mike
    • Marked as answer by Jack_Oh Monday, November 22, 2010 12:29 AM
    Sunday, November 21, 2010 9:25 PM
  • I think the easiest way to generate a random sequence of those four digit numbers is to just pick an entry from the list of all possible values.  Take a look at the following to see what I mean:
     

     Sub Main()
      Dim sets As New List(Of Integer)( _
       {1234, 1243, 1324, 1342, 1423, 1432, _
        2134, 2143, 2314, 2341, 2413, 2431, _
        3124, 3142, 3214, 3241, 3412, 3421, _
        4123, 4132, 4213, 4231, 4312, 4321 })
      Dim r As New Random()
     
      For item As Integer = 1 To 25
       Console.WriteLine(sets(r.Next(0, sets.Count)))
      Next
     
      Console.ReadKey()
     End Sub
     
     

     

    --
    Mike

    What about the N's and F's?

    Subnet Calculator / Planner      Serial Port      Random
    • Marked as answer by Jack_Oh Monday, November 22, 2010 12:29 AM
    Sunday, November 21, 2010 11:04 PM

All replies

  • see next post by me.
    • Edited by dbasnett Sunday, November 21, 2010 5:43 PM
    Sunday, November 21, 2010 4:55 PM
  • I Greatly Appreciate You Help. It appears that the numbers are always being generated 1,2,3,4 although the N and F's seemed to be random. And at the end of 100 tries each number and Letter is paired 25 times. Maybe I should have said I was wanting to generate a random sequence of the numbers 1-4 i.e. 1324 4321 3214 2134. Thanks !
    Sunday, November 21, 2010 5:20 PM
  • Try this one

        Dim PRNG As New Random

        Private Sub Button1_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) Handles Button1.Click

            Dim fn() As String = New String() {"F", "N"}
            Dim tmpRslts As New List(Of String)
            Dim currentSel As String

            For numQ As Integer = 1 To 100
                Dim ctf As Integer = 0
                Dim ctn As Integer = 0
                Dim numP As New List(Of String)
                For x As Integer = 1 To 4
                    numP.Add(x.ToString)
                Next
                'do groups of four
                'each group of four will have two f's and two n's
                For x As Integer = 1 To 4
                    Dim rNumX As Integer = PRNG.Next(0, numP.Count)
                    Dim rNum As String = numP(rNumX)
                    numP.RemoveAt(rNumX)
                    If ctf < 2 AndAlso ctn < 2 Then
                        'could be F or N
                        Dim ForN As Integer = PRNG.Next(0, 2) 'select either
                        currentSel = fn(ForN)
                    ElseIf ctf < 2 Then
                        'must be F
                        currentSel = fn(0)
                    Else
                        'must be N
                        currentSel = fn(1)
                    End If
                    tmpRslts.Add(rNum & "," & currentSel) 'add to temp results
                    'do counts
                    If currentSel = fn(0) Then
                        ctf += 1 'count f
                    Else
                        ctn += 1 'count n
                    End If
                Next
                Stop 'use the debugger to verify that each group of four has two f's and two n's
            Next

            'randomize ;) the results
            Dim theResults As New List(Of String)
            For x As Integer = 0 To tmpRslts.Count - 1
                Dim idx As Integer = PRNG.Next(0, tmpRslts.Count)
                theResults.Add(tmpRslts(idx))
                tmpRslts.RemoveAt(idx)
            Next

            Stop 'use the debugger to verify theResults appear random

        End Sub


    Subnet Calculator / Planner      Serial Port      Random
    • Marked as answer by Jack_Oh Sunday, November 21, 2010 5:44 PM
    Sunday, November 21, 2010 5:42 PM
  • Or

     

    Public Class Form1
        Dim PRNG As New Random

        Private Sub Button1_Click(ByVal sender As System.Object, _
                                  ByVal e As System.EventArgs) Handles Button1.Click

            Dim fn() As String = New String() {"F", "N"}
            Dim nums() As String = New String() {"1", "2", "3", "4"}
            Dim tmpRslts As New List(Of String)

            For numQ As Integer = 1 To 100
                Dim fnL As New List(Of String)
                Dim fnIdx As Integer
                Dim numsL As New List(Of String)
                Dim numsIDX As Integer
                fnL.AddRange(fn) 'add enough F/N
                fnL.AddRange(fn)
                numsL.AddRange(nums) 'add numbers
                Do While numsL.Count > 0
                    fnIdx = PRNG.Next(0, fnL.Count) 'get an FN
                    numsIDX = PRNG.Next(0, numsL.Count) 'get a number
                    tmpRslts.Add(numsL(numsIDX) & "," & fnL(fnIdx)) 'save the results
                    numsL.RemoveAt(numsIDX) 'remove the items just used
                    fnL.RemoveAt(fnIdx)
                Loop

                Stop 'use the debugger to verify that each group of four has two f's and two n's
            Next

            'randomize ;) the results
            Dim theResults As New List(Of String)
            For x As Integer = 0 To tmpRslts.Count - 1
                Dim idx As Integer = PRNG.Next(0, tmpRslts.Count)
                theResults.Add(tmpRslts(idx))
                tmpRslts.RemoveAt(idx)
            Next

            Stop 'use the debugger to verify theResults appear random

        End Sub
    End Class

     

    I like this better than the one picked as the answer.


    Subnet Calculator / Planner       Serial Port       Random
    • Marked as answer by Jack_Oh Monday, November 22, 2010 12:28 AM
    Sunday, November 21, 2010 6:01 PM
  • I think the easiest way to generate a random sequence of those four digit numbers is to just pick an entry from the list of all possible values.  Take a look at the following to see what I mean:
     

     Sub Main()
      Dim sets As New List(Of Integer)( _
       {1234, 1243, 1324, 1342, 1423, 1432, _
        2134, 2143, 2314, 2341, 2413, 2431, _
        3124, 3142, 3214, 3241, 3412, 3421, _
        4123, 4132, 4213, 4231, 4312, 4321 })
      Dim r As New Random()
     
      For item As Integer = 1 To 25
       Console.WriteLine(sets(r.Next(0, sets.Count)))
      Next
     
      Console.ReadKey()
     End Sub
     
     

     

    --
    Mike
    • Marked as answer by Jack_Oh Monday, November 22, 2010 12:29 AM
    Sunday, November 21, 2010 9:25 PM
  • I think the easiest way to generate a random sequence of those four digit numbers is to just pick an entry from the list of all possible values.  Take a look at the following to see what I mean:
     

     Sub Main()
      Dim sets As New List(Of Integer)( _
       {1234, 1243, 1324, 1342, 1423, 1432, _
        2134, 2143, 2314, 2341, 2413, 2431, _
        3124, 3142, 3214, 3241, 3412, 3421, _
        4123, 4132, 4213, 4231, 4312, 4321 })
      Dim r As New Random()
     
      For item As Integer = 1 To 25
       Console.WriteLine(sets(r.Next(0, sets.Count)))
      Next
     
      Console.ReadKey()
     End Sub
     
     

     

    --
    Mike

    What about the N's and F's?

    Subnet Calculator / Planner      Serial Port      Random
    • Marked as answer by Jack_Oh Monday, November 22, 2010 12:29 AM
    Sunday, November 21, 2010 11:04 PM
  • Sorry, I was replying to Jack_Oh's last post, where he said:
     
        Maybe I should have said I was wanting to generate a random sequence of the numbers 1-4 i.e. 1324 4321 3214 2134.
     
    I should have clarified that.

    --
    Mike
    Sunday, November 21, 2010 11:16 PM
  • THANK YOU SO MUCH FOR BEING SO HELPFUL !!!!
    Monday, November 22, 2010 12:29 AM