Solved

vb.net - get random from array

Posted on 2013-05-14
1
434 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 39167123
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 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