0

Is there a simple solution to select random strings in vb.net? I have a list of about twenty paragraphs where three need to go after each other and I want it to be random. Do I have to create a variable? Or is there a command that can be run on a button click?

3 Answers 3

2

One (fairly easy way) to accomplish this would be to have a collection of the paragraphs you want to use, and then use PeanutButter.RandomValueGen from the Nuget package PeanutButter.RandomGenerators (it's open-source too)

RandomValueGen.GetRandomFrom takes a collection of anything and returns a random item from the collection. As a bonus, you can specify a params list of values not to pick, so you can ensure that your paragraphs aren't repeated.

Whilst the library is written in C#, it can obviously be used from any .NET project. There are a lot of other generator methods on RandomValueGen too, if you're interested.

Full disclosure: I'm the author.

Sign up to request clarification or add additional context in comments.

2 Comments

The author of the question from other account, or the author of the library?
Author of the library. Sorry, should have been clearer.
2

If you have a normal list, this should work: If not, write what kind of list you have.

    Dim rn As New Random
    Dim selct As String = lst(rn.Next(0, lst.Count - 1))

selct is the output.

Replace lst with your list name.

12 Comments

The only problem with this approach is that you'll get repeats of paragraphs. If that's not an issue for the OP, then no worries. If it is, my answer provides a zero-effort alternative.
Yes but you must download stuff
install-package peanutbutter.randomgenerators -- less effort than you make it out to be.
Where do you write that?
Open the package manager console -- you can find it via the quick-search in visual studio (Ctrl-Q, I think? But you can see the text box at the very top-right of Visual Studio, and type in "package manager" -- you should get an option for the console). You could also use the GUI Nuget package manager -- I just prefer not to because it's so slow ):
|
0

if you don't want to have a dependency or need to stay on 4.0 for some odd reason or reason X, you can always try this instead

Private rnd As New Random
Public Function GetRandom(input As IEnumerable(Of String), itemToGet As Integer) As List(Of String)
    If input.Count < itemToGet Then
        Throw New Exception("List is too small")
    End If

    Dim copy = input.ToList

    Dim result As New List(Of String)
    Dim item As Integer

    While itemToGet > 0
        item = rnd.Next(0, copy.Count)
        result.Add(copy(item))
        copy.RemoveAt(item)

        itemToGet -= 1
    End While

    Return result
End Function 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.