• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 770
  • Last Modified:

Randomize words...

Hi..

I have a file that contain 10 words (separated by commas) like:
word 1, word2 , word 3, word word word, word5

Now... is there is anyway (may be a VC code, or a macro)...
that will prodcue the same words, separated by commas  but in random order?

In other words...
the output file should look somthing like this:
word 1, word 3 ,word2 , word word word, word5
word2, word 1 ,word5 , word word word,  word 3
word word word, word2 , word 3,word 1 , word5
word5 ,word word word , word2 , word 3, word 1

etc...

Hope that i could explain my question?

Thanks
Mary
0
mary_M
Asked:
mary_M
  • 4
  • 3
  • 2
1 Solution
 
gilbarCommented:
here's a macro that takes the ten words, makes them into a 10 column table, makes another row to the table, reads the original row and randomly puts them into the second row, then deletes the original row, then unmakes them out of a table:
Sub q()
Dim C As Column
Dim X As Long
Dim W As String
    X = 1
    Selection.WholeStory    'grab words
    Selection.Shrink        'dont includ pargraph marker
    Selection.ConvertToTable Separator:=wdSeparateByCommas, NumColumns:=10, _
        NumRows:=1, AutoFitBehavior:=wdAutoFitFixed
    Selection.Copy
    Selection.Paste
    With ActiveDocument.Tables(1)
        Do While X <= .Columns.Count
            .Cell(1, X).Select
            W = Left(Selection, Len(Selection) - 2)
            .Cell(2, Int((10 * Rnd) + 1)).Select
            Selection = W
           X = X + 1
        Loop
        .Rows(1).Delete
        .Select
        End With
    Selection.Rows.ConvertToText Separator:=wdSeparateByCommas, NestedTables:= _
        True
        Selection.HomeKey unit:=wdStory
End Sub

I'd use a better randoming method, as this one can have repetes
0
 
GrahamSkanRetiredCommented:
Are you talking about a text document?
This macro will run in VBA in any office product.

Sub RandomiseWords()
Dim f As Integer
Dim i As Integer
Dim w As Integer
Dim v As Integer
Dim c As Integer
Dim vWords
Dim strNewOrder() As String
Dim strLine As String
Const VariationsCount = 10

f = FreeFile
Open "C:\Myfile.txt" For Input As #f
    Line Input #f, strLine
Close #f
strLine = "1,2,3,4,5,6,7,8"
vWords = Split(strLine, ",")
v = UBound(vWords)
Randomize
For c = 1 To VariationsCount
    ReDim strNewOrder(v)
    For w = 0 To v
        Do
            i = Int((v + 1) * Rnd)
        Loop Until strNewOrder(i) = ""
        strNewOrder(i) = vWords(w)
    Next w
    strLine = Join(strNewOrder, ",")
    MsgBox strLine
Next c
End Sub
0
 
GrahamSkanRetiredCommented:
I see that I left my test line in:
strLine = "1,2,3,4,5,6,7,8"
Please delete it.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
mary_MAuthor Commented:
gilbar & GrahamSkan

I tested both code...  But it is not doing what i want...

It is not recognizing the "phrase"

I mean... i need it to treat each "few words" as one set
if it is separated by ,


IN other words... for:
word one, word tow, word there, word four

I want to get reasukts LIKE THIS:
word one, word there, word tow, word four
word four, word one, word there, word tow
word there, word one, word four,  word tow

But the current codes are displaying results like this:
word one, word word word ,, two four, three


So... i need it to recognize the keyword phrase separated by comma ,

Thanks
Mary
0
 
gilbarCommented:
I my version, converting to table should separate by commas, I'm pretty sure GrahamS' should too. Both aren't working? I'm not sure what the problem is.
0
 
GrahamSkanRetiredCommented:
I've retried mine as well. I created the text file by pasting:

word one, word tow, word there, word four

 into notepad and saving the file.

I ran my code (less the test line: strLine = "1,2,3,4,5,6,7,8") and the phrases remained intact.
0
 
mary_MAuthor Commented:
GrahamSkan ..

But for your code (even after removing the strLine = "1,2,3,4,5,6,7,8"),  it is only *showing* the
randomized phrases...

When i click again to the Myfile.txt file..
i see the SAME phrases in the same
order (in 1 line)...

So, how to save the randomized phrases to the text file?

(it only appear in the dialog, and i click OK, then they aren't
saved in the file!)

Thanks
Mary
0
 
mary_MAuthor Commented:
GrahamSkan ..

But for your code (even after removing the
strLine = "1,2,3,4,5,6,7,8"),  it is only *showing* the
randomized phrases...

When i click again to the Myfile.txt file..
i see the SAME phrases in the same
order (in 1 line)...

So, how to save the randomized phrases to the text file?

(it only appear in the dialog, and i click OK, then they
aren't
saved in the file!)

Thanks
Mary
0
 
GrahamSkanRetiredCommented:
Sub RandomiseWords()
Dim f As Integer
Dim i As Integer
Dim w As Integer
Dim v As Integer
Dim c As Integer
Dim vWords
Dim strNewOrder() As String
Dim strLine As String
Const VariationsCount = 10

f = FreeFile
'read input file
Open "C:\Myfile.txt" For Input As #f
    Line Input #f, strLine
Close #f
'strLine = "1,2,3,4,5,6,7,8"
vWords = Split(strLine, ",")
v = UBound(vWords)
Randomize
f = FreeFile
'write variationscount number of variations
Open "C:\MyRandomisedFile.txt" For Output As #f
For c = 1 To VariationsCount
    ReDim strNewOrder(v)
    For w = 0 To v
        Do
            i = Int((v + 1) * Rnd)
        Loop Until strNewOrder(i) = ""
        strNewOrder(i) = vWords(w)
    Next w
    strLine = Join(strNewOrder, ",")
    Print #f, strLine
Next c
Close #f
End Sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now