Question VB Word Jumbler, not scrambler :)

matty

Member
Joined
Oct 11, 2008
Messages
5
Programming Experience
Beginner
Hello all,

I'm not the best when it comes to this, but i've been banging my head for days and i think its time i try to ask some of the very talented ppl i see on this site a quick question.
I stumbled across this post from about 2 months ago...
http://www.vbdotnetforums.com/vb-net-general-discussion/28669-basic-word-jumber.html

What that post is about is almost exactly what i need...except i dont want the characters in the individual words to be changed, i just need the whole words moved around. For example a text file/box that contains this "Jim Tire Tall2 G'night" would be "jumbled" or rearanged randomly to "Tall2 Jim G'night Tire". So far i have this code done, but it only gives me one word at a time, not all the words all at once.

VB.NET:
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Dim MyArray() As String = Split(testtxtbox1.Text, " ")
        Dim I As Integer
        Dim Index1 As Integer
        Dim Index2 As Integer
        Dim Temp As String

        For I = 1 To 20
            Index1 = Int(Rnd() * 10)
            Index2 = Int(Rnd() * 10)
            While Index1 = Index2
                Index2 = Int(Rnd() * 10)
            End While
            Temp = MyArray(Index1)
            MyArray(Index1) = MyArray(Index2)
            MyArray(Index2) = Temp
        Next I

        testtxtbox2.Text = Temp


    End Sub

One last thing....lets say there are 1000 words in this text file that i want to "jumble" up - but out of those 1000 words i specify in the program that i want only 500 random words out of that 1000 word file. Any ideas? Thanks in advance everyone!

Matt
 
Same concept, almost same code:
VB.NET:
Private r As New Random
Private Function RandomizeWords(ByVal text As String) As String
    Dim words As New List(Of String)(text.Split(" "c))
    Dim newwords As New List(Of String)
    For Each word As String In words
        newwords.Insert(r.Next(0, newwords.Count + 1), word)
    Next
    Return String.Join(" ", newwords.ToArray)
End Function
One last thing....lets say there are 1000 words in this text file that i want to "jumble" up - but out of those 1000 words i specify in the program that i want only 500 random words out of that 1000 word file. Any ideas? Thanks in advance everyone!
For example take 500 random picks from that "words" variable in example, remove the picked item as you go.
 
Same concept, almost same code:
VB.NET:
Private r As New Random
Private Function RandomizeWords(ByVal text As String) As String
    Dim words As New List(Of String)(text.Split(" "c))
    Dim newwords As New List(Of String)
    For Each word As String In words
        newwords.Insert(r.Next(0, newwords.Count + 1), word)
    Next
    Return String.Join(" ", newwords.ToArray)
End Function

For example take 500 random picks from that "words" variable in example, remove the picked item as you go.


Thanks John!
Im stuck agian tho, here are the basics on how i'm trying to implement this code.

User opens a .txt file and the contents are sent to global variable "content1". Next the user enters in how many randomized words he wants in textbox "totalwords". All the above works with no errors, from here is where i need your help.

Once a numerical number is entered the user clicks "button12" and that button is to run that code you gave me, thus jumbling whats inside variable "content1" and taking out however many words was entered into the variable "totalwords". Once the code you gave me has jumbled up enough words to satisfy the number defined in "totalwords", the jumbled words are to be sent to a global variable named "scrambled1".

And thats it! The code you gave me works perfect, except for some reason i just cant link the "button12" to activate it and put the radomized words (matching the "totalwords" variable) into the global variable "scrambled1". Thanks!
 
Random lenght of text also.

Hey there.

I want to hear if there is a way to do this so that it dosen't just mix up the word (ex. word to be mixed: hejlol end up like llojeh). I want to know if its possible to like only make words with 3 letters or 4 and so on. Like word to be mixed: helloa ends up like lol or hello so that it makes many different words in different lenght's?

Hope you know what i mean :)

Kind Regards
OutRager-
 
For example take 500 random picks from that "words" variable in example, remove the picked item as you go.

Hey John,

I've figured out the rest to my last question, but i'm still bangin my head on trying to "pick" x amount of words out of the "words" variable. Any suggestions? Thanks!
Matt
 
matty, learn how to use a For-Next loop.
 
Back
Top