Solved

vb.net - get random from array

Posted on 2013-05-14
1
429 Views
Last Modified: 2013-05-14
Hello,
I am trying to get 3 results when I call showRand() function.
how can I make something so that I dont get the same number twice?

        Dim numbs() As String = {"1", "2", "3", "11", "12", "13"}
        Debug.Print(showRand(numbs, 3))
        
    
    Function showRand(ByRef data() As String, Optional ByVal MoreThanOne As Integer = 0) As String
        Dim Result As String = Nothing
        Dim Rand As New Random

        If MoreThanOne > 0 Then
            For i = 1 To MoreThanOne
                Result += data(Rand.Next(0, data.GetUpperBound(0) + 1)) & ","
            Next
            Result = Result.Substring(0, Result.Length - 1)
        Else
            Result = data(Rand.Next(0, data.GetUpperBound(0) + 1))
        End If

        Return Result
    End Function

Open in new window

0
Comment
Question by:XK8ER
1 Comment
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
Comment Utility
One way...
    Function showRand(ByVal data() As String, Optional ByVal MoreThanOne As Integer = 0) As String
        Dim Result As String = Nothing
        Static Rand As New Random

        If MoreThanOne > 0 Then
            If MoreThanOne <= data.Length Then
                Dim index As Integer
                Dim strings As New List(Of String)(data)
                Dim selected As New List(Of String)
                For i As Integer = 1 To MoreThanOne
                    index = Rand.Next(0, strings.Count)
                    selected.Add(strings(index))
                    strings.RemoveAt(index)
                Next
                Result = String.Join(",", selected.ToArray)
            End If
        Else
            Result = data(Rand.Next(0, data.Length))
        End If

        Return Result
    End Function

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now