7 Replies - 1942 Views - Last Post: 16 March 2011 - 05:20 PM Rate Topic: -----

#1 Barthoid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-October 10

using randomly generated numbers from a list only once

Posted 16 March 2011 - 05:50 AM

Below is a form which produces a graph, the idea is to produce 6 random numbers from a range (0,6) place the numbers in an array then use these numbers to produce columns on the graph, however at the moment it is using the numbers more than once, how can I get it to just use the values once only in the array.


  Private Sub btnGraph_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGraph.Click

        txtAns.Text = ""
        g.Clear(Me.BackColor)

        g.DrawLine(Pens.Black, 50, 70, 50, 420)
        g.DrawLine(Pens.Black, 50, 420, 410, 420)

        'Create an Array to hold six  numbers


        Dim heights(i) As Integer
        Dim intCount As Integer

        'Create a random object


        'Fill the array with random numbers
        

        For intCount = 0 To i
            heights(intCount) = rand.Next(0, 6) * 60
        Next

        For i As Integer = 0 To 5

            Dim size As Integer = heights(i)

            g.FillRectangle(New SolidBrush(colours(i)), (i * 50) + 52, 420 - size, 40, size)
            g.DrawRectangle(Pens.Black, (i * 50) + 52, 420 - size, 40, size)

        Next



        g.DrawString("1", New Font("Comic Sans Ms", 16), Brushes.Black, 60, 420)
        g.DrawString("2", New Font("Comic Sans Ms", 16), Brushes.Black, 110, 420)
        g.DrawString("3", New Font("Comic Sans Ms", 16), Brushes.Black, 160, 420)
        g.DrawString("4", New Font("Comic Sans Ms", 16), Brushes.Black, 210, 420)
        g.DrawString("5", New Font("Comic Sans Ms", 16), Brushes.Black, 260, 420)
        g.DrawString("6", New Font("Comic Sans Ms", 16), Brushes.Black, 310, 420)

        Lblh.Text = " Column 1 = " & heights(0) & "," & " Column 2 = " & heights(1) & "," & " Column 3 = " & heights(2) & "," & " Column 4 = " & heights(3) & "," & " column 5 = " & heights(4) & "," & " Column 6 = " & heights(5)


        Dim highest As Integer = heights(0)
        Dim lowest As Integer = heights(0)
        Dim total As Integer = heights(0)
        Dim highPos As Integer = 0
        Dim lowPos As Integer = 0
        Dim count As Integer = 0


        For i As Integer = 0 To 5
            If heights(i) > highest Then
                highest = heights(i)
                highPos = i
            End If

            If heights(i) < lowest Then
                lowest = heights(i)
                lowPos = i
            End If
            total = total + heights(i)

            If heights(i) > 0 Then
                count = count + 1
            End If
        Next


        Dim qType As Integer = rand.Next(1, 4)

        Select Case qType
            Case 1
                lblQuestion.Text = " How many columns are there" & vbNewLine & "containing data"
                ExpectedAns = count

            Case 2
                lblQuestion.Text = " Which column is the Highest "
                ExpectedAns = highPos + 1


            Case 3
                lblQuestion.Text = " Which column is the lowest "
                ExpectedAns = lowPos + 1

                '        Case 4
                '            lblQuestion.Text = " Which column is between" & vbNewLine & "75 and 150 high "
                '            'ExpectedAns = lblF.Text

        End Select
    End Sub

    Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
        'txtAns.Text = ExpectedAns
        If (txtAns.Text = ExpectedAns) Then
            Well_Done.ShowDialog()
        Else
            Try_Again.ShowDialog()


        End If
    End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: using randomly generated numbers from a list only once

#2 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: using randomly generated numbers from a list only once

Posted 16 March 2011 - 12:22 PM

So, what you're saying is that you want to generate 6 values out of a possible 7, and in random order. All you need to do is keep track of the random numbers already generated, and get another one if the one you get is already taken. Put a loop inside a for, so:
Dim x(6) As Integer
Dim y As Integer
For i As Integer = 0 To 5
    Do
        y = Random.Next(0, 6)
    Loop Until x(y) <> 1
    x(y) = 1
    Debug.Write y
Next i
Debug.WriteLine
This code (untested, got no Vb.Net platform for a while yet, but tested fine in VB6 before I rewrote it for Vb.Net) should generate 6 discrete random numbers out of a pool of 7 (that is, 0 to 6).
Was This Post Helpful? 2
  • +
  • -

#3 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: using randomly generated numbers from a list only once

Posted 16 March 2011 - 12:27 PM

Just a little add:

        Dim x(6) As Integer
        Dim y As Integer
        Dim r As New Random(System.DateTime.Now.Millisecond)
        For i As Integer = 0 To 5
            Do
                y = r.Next(0, 6)
            Loop Until x(y) <> 1
            x(y) = 1
            Debug.Write(y)
        Next i


Was This Post Helpful? 0
  • +
  • -

#4 Barthoid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 27-October 10

Re: using randomly generated numbers from a list only once

Posted 16 March 2011 - 01:04 PM

View PostBarthoid, on 16 March 2011 - 05:50 AM, said:

Below is a form which produces a graph, the idea is to produce 6 random numbers from a range (0,6) place the numbers in an array then use these numbers to produce columns on the graph, however at the moment it is using the numbers more than once, how can I get it to just use the values once only in the array.


  Private Sub btnGraph_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGraph.Click

        txtAns.Text = ""
        g.Clear(Me.BackColor)

        g.DrawLine(Pens.Black, 50, 70, 50, 420)
        g.DrawLine(Pens.Black, 50, 420, 410, 420)

        'Create an Array to hold six  numbers


        Dim heights(i) As Integer
        Dim intCount As Integer

        'Create a random object


        'Fill the array with random numbers
        

        For intCount = 0 To i
            heights(intCount) = rand.Next(0, 6) * 60
        Next

        For i As Integer = 0 To 5

            Dim size As Integer = heights(i)

            g.FillRectangle(New SolidBrush(colours(i)), (i * 50) + 52, 420 - size, 40, size)
            g.DrawRectangle(Pens.Black, (i * 50) + 52, 420 - size, 40, size)

        Next



        g.DrawString("1", New Font("Comic Sans Ms", 16), Brushes.Black, 60, 420)
        g.DrawString("2", New Font("Comic Sans Ms", 16), Brushes.Black, 110, 420)
        g.DrawString("3", New Font("Comic Sans Ms", 16), Brushes.Black, 160, 420)
        g.DrawString("4", New Font("Comic Sans Ms", 16), Brushes.Black, 210, 420)
        g.DrawString("5", New Font("Comic Sans Ms", 16), Brushes.Black, 260, 420)
        g.DrawString("6", New Font("Comic Sans Ms", 16), Brushes.Black, 310, 420)

        Lblh.Text = " Column 1 = " & heights(0) & "," & " Column 2 = " & heights(1) & "," & " Column 3 = " & heights(2) & "," & " Column 4 = " & heights(3) & "," & " column 5 = " & heights(4) & "," & " Column 6 = " & heights(5)


        Dim highest As Integer = heights(0)
        Dim lowest As Integer = heights(0)
        Dim total As Integer = heights(0)
        Dim highPos As Integer = 0
        Dim lowPos As Integer = 0
        Dim count As Integer = 0


        For i As Integer = 0 To 5
            If heights(i) > highest Then
                highest = heights(i)
                highPos = i
            End If

            If heights(i) < lowest Then
                lowest = heights(i)
                lowPos = i
            End If
            total = total + heights(i)

            If heights(i) > 0 Then
                count = count + 1
            End If
        Next


        Dim qType As Integer = rand.Next(1, 4)

        Select Case qType
            Case 1
                lblQuestion.Text = " How many columns are there" & vbNewLine & "containing data"
                ExpectedAns = count

            Case 2
                lblQuestion.Text = " Which column is the Highest "
                ExpectedAns = highPos + 1


            Case 3
                lblQuestion.Text = " Which column is the lowest "
                ExpectedAns = lowPos + 1

                '        Case 4
                '            lblQuestion.Text = " Which column is between" & vbNewLine & "75 and 150 high "
                '            'ExpectedAns = lblF.Text

        End Select
    End Sub

    Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
        'txtAns.Text = ExpectedAns
        If (txtAns.Text = ExpectedAns) Then
            Well_Done.ShowDialog()
        Else
            Try_Again.ShowDialog()


        End If
    End Sub
End Class



I am unsure where to place this code snippet at the moment I have placed it between For i As Integer and Dim size As integer but it hasn't changed the result please help
Was This Post Helpful? 0
  • +
  • -

#5 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: using randomly generated numbers from a list only once

Posted 16 March 2011 - 03:13 PM

View PostNoBrain, on 16 March 2011 - 08:27 PM, said:

Just a little add:

        Dim x(6) As Integer
        Dim y As Integer
        Dim r As New Random(System.DateTime.Now.Millisecond)
        For i As Integer = 0 To 5
            Do
                y = r.Next(0, 6)
            Loop Until x(y) <> 1
            x(y) = 1
            Debug.Write(y)
        Next i


Nobrain, I'm not sure why your code is an improvement. Can you explain?

This post has been edited by BobRodes: 16 March 2011 - 03:37 PM

Was This Post Helpful? 0
  • +
  • -

#6 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Re: using randomly generated numbers from a list only once

Posted 16 March 2011 - 04:06 PM

its not improvement you cant use random without init. it.
Was This Post Helpful? 0
  • +
  • -

#7 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: using randomly generated numbers from a list only once

Posted 16 March 2011 - 04:18 PM

Ok. What you say as to where you placed the code will indeed cause it to do nothing. You'll need to use the idea to fix your code in lines 21 to 23. (They don't work at all unless they're not the same code as you posted, by the way.)
Was This Post Helpful? 0
  • +
  • -

#8 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 574
  • View blog
  • Posts: 2,989
  • Joined: 19-May 09

Re: using randomly generated numbers from a list only once

Posted 16 March 2011 - 05:20 PM

View PostNoBrain, on 17 March 2011 - 12:06 AM, said:

its not improvement you cant use random without init. it.

Oops. Not so good. Shouldn't reinitialize in the loop either, even if you could do it that way. Thanks Nobrain.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1