locked
Random numbers to array RRS feed

  • Question

  • Hi Everyone

    I have been trying to figure out the best way to the rnd() to display a different string. I was using this at first.
    Dim x As String
    Dim i As Integer
    i = CInt(Math.Floor((49 - 0) + 1) * Rnd()) + 0
    x = MessageBox.Show(str(i))
    But that would use the same random numbers everytime the program was opened. I read that I needed to add the Randomize() function in to make it use different random numbers when the progam is ran, and that seemed to work. The problem I am getting now is that I seemed to get the same random numbers more often now. So i tend to get a lot more repeats in the strings being displayed. I want the string to be called in a random oeder when the program is opened, but I dont need them to repeat. So I was thinking that I should use the random function to  populate an array then use the numbers from the array to call string in the messagebox. The problem with doing this is that I am not sure how to scan the array to if this number has already been added.

    So my question would be does this look like the best solution? And if so would there be a guide showing me how to scan the array to make sure the same random number does not get added.
    Friday, July 24, 2009 2:13 AM

Answers

  • If you only want to display each string once and ensure that you display one each time you call fact() try this:

    Public Class Form1
        Dim Strings(14) As String
        Dim NumberList As New List(Of Integer)
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' Fill Strings with numbers just for testing
            For count As Integer = 0 To 14
                Strings(count) = ""
                Strings(count) = (count + 1).ToString
                NumberList.Add(count)
            Next
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If NumberList.Count > 0 Then
                fact()
            Else
                MsgBox("None left")
            End If
        End Sub
        Sub fact()
            Dim Rnd As New Random
            Dim Index As Integer = Rnd.Next(0, NumberList.Count)
            MsgBox(Strings(NumberList(Index)))
            NumberList.RemoveAt(Index)
        End Sub
    End Class

    • Marked as answer by Anthony_02 Friday, July 24, 2009 9:40 PM
    Friday, July 24, 2009 10:36 AM

All replies

  • Use Random class

    Dim x As String
    Dim r as new Random
    Dim rnum as Integer=r.Next(1,7) ' this will generate any number between 1 and 7, 7 is not included
    Dim i As Integer
    i = CInt(Math.Floor((49 - 0) + 1) * Rnd()) + 0
    x = MessageBox.Show(str(i))
    

    kaymaf

     


    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    Friday, July 24, 2009 2:38 AM
  • This does help but I am still havng one problem. I will try to exlpain the it better this time. Here is the code I am using.
     Sub fact()
            Dim str(15) As String
            str(1) = ""
            str(2) = ""
            str(3) = ""
            str(4) = ""
            str(5) = ""
            str(6) = ""
            str(7) = ""
            str(8) = ""
            str(9) = ""
            str(10) = ""
            str(11) = ""
            str(12) = ""
            str(13) = ""
            str(14) = ""
            str(15) = ""
                    
            Dim x As String
            Dim i As Integer
            Dim r As New Random
            Dim rnum As Integer = r.Next(1, 16)
            'i = CInt(Math.Floor((49 - 0) + 1) * Rnd()) + 0
            x = MessageBox.Show(str(rnum))
        End Sub
    The random class fixed the problem of the same random numbers everytime the program is opened.

    What I would like the sub() to do is display the strings in a random order like it is doing, but do not repeat displaying a string that has already been sent to the messagebox untill all strings have been shown with the messagebox. Then it can reset and display them in a random order again.
    Friday, July 24, 2009 3:33 AM
  • I use arraylist to store the generated number, if the arraylist contain the number, it will not display messagebox. also make sure when you reset the the sub fact, try to clear the arraylist by using
    Arr.Clear()
    Dim arr as new arrayList
    
    Sub fact()
            Dim str(15) As String
            str(1) = ""
            str(2) = ""
            str(3) = ""
            str(4) = ""
            str(5) = ""
            str(6) = ""
            str(7) = ""
            str(8) = ""
            str(9) = ""
            str(10) = ""
            str(11) = ""
            str(12) = ""
            str(13) = ""
            str(14) = ""
            str(15) = ""
                    
            Dim x As String
            Dim i As Integer
            Dim r As New Random
            Dim rnum As Integer = r.Next(1, 16)
        If not arr.contains(rnum) Then 'if arraylist contain the number, no messagebox will display
            'i = CInt(Math.Floor((49 - 0) + 1) * Rnd()) + 0
            x = MessageBox.Show(str(rnum))
    arr.Add(rnum)
         End if
        End Sub
    
    kaymaf
    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    • Edited by kaymaf Friday, July 24, 2009 4:48 AM
    Friday, July 24, 2009 4:27 AM
  • If you only want to display each string once and ensure that you display one each time you call fact() try this:

    Public Class Form1
        Dim Strings(14) As String
        Dim NumberList As New List(Of Integer)
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' Fill Strings with numbers just for testing
            For count As Integer = 0 To 14
                Strings(count) = ""
                Strings(count) = (count + 1).ToString
                NumberList.Add(count)
            Next
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If NumberList.Count > 0 Then
                fact()
            Else
                MsgBox("None left")
            End If
        End Sub
        Sub fact()
            Dim Rnd As New Random
            Dim Index As Integer = Rnd.Next(0, NumberList.Count)
            MsgBox(Strings(NumberList(Index)))
            NumberList.RemoveAt(Index)
        End Sub
    End Class

    • Marked as answer by Anthony_02 Friday, July 24, 2009 9:40 PM
    Friday, July 24, 2009 10:36 AM