0

I'm trying to determine the proper syntax for displaying a random key on each label.

display random sorted list key on label

//declare random
Random rnd = new Random();

//create the sorted list and add items
SortedList<string,string> sl = new SortedList<string,string>();
sl.Add("PicknPay", "jam");
sl.Add("Spar", "bread");
sl.Add("Checkers", "rice");
sl.Add("Shoprite", "potato");
sl.Add("Cambridge", "spinash");

int Count = 0;
int nValue = rnd.Next(5);
int newindex = 0;
int seekindex;

for (seekindex = 0; seekindex > nValue; seekindex++)
{
    newindex =  rnd.Next(seekindex);
}

lbl1.Text = "";

foreach (var item in sl.Keys) 
{
    lbl1.Text += "," + Convert.ToString(item.IndexOf(item));
}

lbl1.Text = lbl1.Text.TrimStart(',');
Rufus L
  • 36,127
  • 5
  • 30
  • 43
MO_Tries
  • 9
  • 4
  • Are you trying to choose a unique, random key from your `SortedList` to display in each of the three labels? Your code indicates that you're trying to display all of them, separated by commas, in `lbl1.Text`. – Rufus L Jun 07 '18 at 18:05
  • I'm not sure if I understand this correctly. Do you want to display each `Key` in `sl` in a label of its own? – Sach Jun 07 '18 at 18:05
  • In case you want randomly shuffle you sorted list you could use linq order by in your second for each https://stackoverflow.com/a/4262134/3254405 – boateng Jun 07 '18 at 18:07
  • when the form loads . the three labels are supposed to display a Randomly selected key FROM the sorted list ...for example when form loads label 1 will display "spar" and label 2 display "shopright" ,label3 displays cambridge but if the form loads again then should pick different keys from the above randomly – MO_Tries Jun 07 '18 at 18:10

1 Answers1

1

One way to do this would be get a randomly ordered list of the keys by calling the System.Linq extension method OrderBy and passing it the value returned from Random.Next(), then take the first three items from this shuffled list:

SortedList<string, string> sl = new SortedList<string, string>
{
    {"PicknPay", "jam"},
    {"Spar", "bread"},
    {"Checkers", "rice"},
    {"Shoprite", "potato"},
    {"Cambridge", "spinash"}
};

var rnd = new Random();
var shuffledKeys = sl.Keys.OrderBy(key => rnd.Next()).ToList();

lbl1.Text = shuffledKeys[0];
lbl2.Text = shuffledKeys[1];
lbl3.Text = shuffledKeys[2];
Rufus L
  • 36,127
  • 5
  • 30
  • 43
  • I didn't know about the `OrderBy(key => rnd.NextDouble())`. Neat trick, will add to my 'Tips and Tricks', thanks! – Sach Jun 07 '18 at 18:20
  • Thank you @Rufus L Thank you, you legend you , you made so simple .. my textbooks are pretty useless they do not explain properly . i humbly request you refer me to a book that can properly teach me what i need to learn. – MO_Tries Jun 07 '18 at 18:22
  • One question though, why `NextDouble()` and not `Next()`? – Sach Jun 07 '18 at 18:25
  • 1
    @MO_Tries I'm afraid I cannot recommend any books at the moment - I mostly learn on the job and online. My suggestion is to keep playing with small projects, ask lots of questions, and you will be successful! – Rufus L Jun 07 '18 at 18:27
  • 1
    @Sach `Next()` works too, and it's probably a little more efficient. I'll change the sample. – Rufus L Jun 07 '18 at 18:35