0

I want to build a sequence randomiser mobile app in c#. I would like to retrieve the smallest and the largest number in an interval from two diffrent text boxes, and after I click the Generate button to display the random sequence of all the numbers in a new text box.

My code only displays one number. What's wrong with it?

Thanks.

 public sealed partial class MainPage : Page
{
    public MainPage()
    {
       this.InitializeComponent();           
    }
   private void button_Click(object sender, RoutedEventArgs e)
    {
        int seed = 345;
        var result = "";
        int min = Convert.ToInt32(textBox.Text);
        int max = Convert.ToInt32(textBox2.Text);
       Random r3 = new Random(seed);
        for (int i = min; i < max; i++)
        {
            ecran.Text = (/*"," + r3.Next(min, max)*/i).ToString();
        }
    }
ISS
  • 406
  • 6
  • 24

2 Answers2

4

To clarify what was wrong with your solution:

Inside the loop you were constantly reassigning the value of ecran.Text.

i.e.

1st loop cycle      > ecran.Text = ", " + 77
2nd loop cycle      > ecran.Text = ", " + 89

//Value of ecran.Text after 1st cycle is ", 77"
//Value of ecran.Text after 2nd cycle is ", 89"

Overriding the value of ecran.Text with each iteration.

Fixed by adding a plus symbol in front of equals ecran.Text += ", " + LOGIC

Rinor
  • 1,790
  • 13
  • 22
2

This happens because you assign sequence values to ecran.Text in a loop. Instead, you should create a string representation of the sequence, and assign it at the end.

Use Shuffle<T> method from this Q&A:

int min = Convert.ToInt32(textBox.Text);
int max = Convert.ToInt32(textBox2.Text);
if (max < min) return;
var sequence = Enumerable.Range(min, max-min+1).ToList();
Shuffle(sequence);
ecran.Text = string.Join(",", sequence);
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    your solution does not allow numbers to duplicate. Maybe it would better to use something like this: `var sequence = Enumerable.Range(min, max - min + 1).Select(i => r3.Next(min, max));`? – Aleks Andreev Jul 29 '17 at 15:25
  • @AleksAndreev From OP's incorrect code it appears that he wants a "random sequence of all the numbers" in the min-max range. Otherwise, a single count would be sufficient. – Sergey Kalinichenko Jul 29 '17 at 15:31
  • Thanks, but I can't seem to make it work. It underlines in red Shuffle and Jon. – ISS Jul 29 '17 at 15:54
  • @ dasblinkenlight Yes. That's what I really want. – ISS Jul 29 '17 at 15:54
  • 2
    @SilviuIsidor You need to copy `Shuffle` from the Q&A that I linked to the question. – Sergey Kalinichenko Jul 29 '17 at 15:55
  • @SilviuIsidor Did you follow the link? – Sergey Kalinichenko Jul 29 '17 at 16:04
  • So I used this `public static IList NextList(this Random r, IEnumerable source) {var list = new List(); foreach (var item in source) { var i = r.Next(list.Count + 1); if (i == list.Count) { list.Add(item);} else { var temp = list[i]; list[i] = item; list.Add(temp); } } return list; }` How do I make `ecran.Text` display the list? – ISS Jul 29 '17 at 16:16
  • 2
    @SilviuIsidor See the assignment on the last line of my answer. – Sergey Kalinichenko Jul 29 '17 at 16:19
  • @Aleks Andreev Your solutions does not generate random sequence but a random list of numbers. The problem is that I get the same numbers twice or many times. I want a shuffled list. – ISS Jul 29 '17 at 16:49
  • @dasblinkenlight This worked: `/* generate the sequence */ Random rnd = new Random(); var sequence = Enumerable.Range(min, max).OrderBy(n => rnd.Next()); /* write down the sequence */ ecran.Text = string.Join(",", sequence);` – ISS Jul 29 '17 at 17:10