2
votes

Well, I've been trying to randomize the lines of my richtextbox, and it's starting to bug me now as I'm using the random but it's not working... When I use this it'll repeat some lines multiple times, and not include some at all...

I've looked at: RichTextBox - sorting lines randomly

But it didn't help me at all.

RichtextBox rtb = new RichTextBox();    
Random R = new Random();
int y;
rtb.Text = "";
for (int i = 0; i < richTextBox1.Lines.Length; i++)
{
   y = R.Next(0, richTextBox1.Lines.Length);
   rtb.Text = rtb.Text + richTextBox1.Lines[y].ToString() + "\r\n";
}
richTextBox2.Text = rtb.Text;

For example if I input,

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam in aliquam enim. Proin at lacus magna. Nam bibendum, augue non semper fringilla, ante est interdum ipsum, a egestas urna dolor vel risus. Phasellus eget dui non augue pretium ullamcorper at ac tellus. Sed mattis risus sit amet metus dictum egestas. Phasellus tempus blandit enim, ac congue urna bibendum sed. Sed iaculis pulvinar dui vel tristique. Etiam justo metus, consequat in pellentesque eu, eleifend id nunc.

It gives me,

Sed mattis risus sit amet metus dictum egestas. Phasellus tempus blandit enim, ac congue urna bibendum sed. Nam bibendum, augue non semper fringilla, ante est interdum ipsum, a egestas urna dolor vel risus. Sed mattis risus sit amet metus dictum egestas. Phasellus tempus blandit enim, ac congue urna bibendum sed. Sed iaculis pulvinar dui vel tristique. Etiam justo metus, consequat in pellentesque eu, eleifend id nunc. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam in aliquam enim. Proin at lacus magna.

Where one line is repeated, and one missed out... With longer text it occurs more often.

I haven't been doing C# too long, so sorry if this is simple. :/

2
The answer to the question you linked appears to do exactly what you want - why did it not help you?Blorgbeard
So, you want the lines in random order, not repeated, and must eventually provide all lines? Like dealing a deck of cards ...???IAbstract
What you're looking for is a shuffle algorithm.Rob I
@Blorgbeard the code on there didn't actually work for me... I tested it and modified it multiple times...Kieran Boyle
@Robl I'll check that out, thanks.Kieran Boyle

2 Answers

2
votes

Here is your logic in pseudo code:

Foreach line in original
Choose a random line
Add it to the output

You never consider what lines you have already outputted, thus don't know to not pick them next time.

Notice that in the link you provided they recommend first extracting the lines into a structure and then mutating that structure to provide the new ordering. This ensures you end up with each line once.

There are several algorithms for shuffling in place, the simplest being:

Select the first item
Select a random line, after or including the previous selection
Swap the two selections
Repeat moving the first selection to the left
1
votes

Random means "just" that - it will return a number between 0 and the (number of lines in your rich text box - 1). There is no guarentee of uniqueness (and in fact it will be quite unlikely).

If you want to get them in a unique order, you'll have to keep track of the ones that are used, and not use them again.

A simple example (not necessarily efficient)

   List<int> usedLineIndexes = new List<int>();

   while (usedLineIndexes.Count < richTextBox1.Lines.Length)
   {
        int y = R.Next(0, richTextBox1.Lines.Length);
        if (usedLineIndexes.Contains(y))
            continue;   // Try again


        usedLineIndexes.Add(y);

        rtb.Text = rtb.Text + richTextBox1.Lines[y].ToString() + "\r\n";
   }

Again, this is untested, inefficient code, but it will give you the idea.