9 Replies - 411 Views - Last Post: 26 November 2013 - 07:25 AM Rate Topic: -----

#1 codecudder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 23-November 13

New Random List

Posted 24 November 2013 - 02:55 PM

Hello there, I am having difficulties on creating a new random list of these buttons in wpf. How do I get a new list of these buttons in a random order? I'd love some help, thanks!



            Random ran = new Random();
            List<Button> listOfButtons = new List<Button>();
            listOfButtons.Add(buttonOne);
            listOfButtons.Add(buttonTwo);
            listOfButtons.Add(buttonThree);



Is This A Good Question/Topic? 0
  • +

Replies To: New Random List

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3452
  • View blog
  • Posts: 10,655
  • Joined: 05-May 12

Re: New Random List

Posted 24 November 2013 - 03:05 PM

Did you look at shuffling algorithms? It' basically the same concept.
Was This Post Helpful? 0
  • +
  • -

#3 codecudder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 23-November 13

Re: New Random List

Posted 24 November 2013 - 03:30 PM

View PostSkydiver, on 24 November 2013 - 03:05 PM, said:

Did you look at shuffling algorithms? It' basically the same concept.


Yes, but I am confused on how to go about it because I'm using "buttons." I get the error cannot implicitly convert system.windows.control to int
 for (int n = listOfButtons.Count - 1; n > 0; --n)
            {
                int k = ran.Next(n + 1);
                int temp = listOfButtons[n]; //cannot implicitly convert system.windows.control to int 
                listOfButtons[n] = listOfButtons[k];
                listOfButtons[k] = temp;
            }


Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3452
  • View blog
  • Posts: 10,655
  • Joined: 05-May 12

Re: New Random List

Posted 24 November 2013 - 03:40 PM

Change int on line 4 to Button.

It should be obvious that the reason for this is because you are swapping buttons, not integers.
Was This Post Helpful? 0
  • +
  • -

#5 codecudder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 23-November 13

Re: New Random List

Posted 24 November 2013 - 06:28 PM

View PostSkydiver, on 24 November 2013 - 03:40 PM, said:

Change int on line 4 to Button.

It should be obvious that the reason for this is because you are swapping buttons, not integers.

Oh yes I see but then it says the opposite

 for (int n = listOfButtons.Count - 1; n > 0; --n) //cannot convert int to system.control.windows.button
            {
               Button k = ran.Next(n + 1);
               Button temp = listOfButtons[n];
                listOfButtons[n] = listOfButtons[k];
                listOfButtons[k] = temp;
            }



Was This Post Helpful? 0
  • +
  • -

#6 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 548
  • View blog
  • Posts: 2,462
  • Joined: 03-December 12

Re: New Random List

Posted 24 November 2013 - 06:55 PM

for (int n = 0, listOfButtons.Count - 1 != n; n++) //cannot convert int to system.control.windows.button

This post has been edited by astonecipher: 24 November 2013 - 06:56 PM

Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3452
  • View blog
  • Posts: 10,655
  • Joined: 05-May 12

Re: New Random List

Posted 24 November 2013 - 07:44 PM

The index on line 1 is supposed to be an integer. You use integers to index into an array or a list.

Your array or list contains buttons. To get a reference to a button, you need a variable of type button.
Was This Post Helpful? 0
  • +
  • -

#8 codecudder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 23-November 13

Re: New Random List

Posted 25 November 2013 - 06:03 PM

View PostSkydiver, on 24 November 2013 - 07:44 PM, said:

The index on line 1 is supposed to be an integer. You use integers to index into an array or a list.

Your array or list contains buttons. To get a reference to a button, you need a variable of type button.

Oh okay, but how would implement this into shuffling to make a new list. Do you mean button btt = new button();?
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3452
  • View blog
  • Posts: 10,655
  • Joined: 05-May 12

Re: New Random List

Posted 26 November 2013 - 06:53 AM

Following the same concept as the inplace shuffling. Instead of placing the picked card at the end of the current list, add the picked card to a new list.
Was This Post Helpful? 0
  • +
  • -

#10 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 267
  • View blog
  • Posts: 547
  • Joined: 07-July 10

Re: New Random List

Posted 26 November 2013 - 07:25 AM

View PostSkydiver, on 26 November 2013 - 08:53 AM, said:

Following the same concept as the inplace shuffling. Instead of placing the picked card at the end of the current list, add the picked card to a new list.

[PseudoCode]
create new suffledList
while originalList has items
{
index = random number between 0 and originalList.Length
add originalList[index] to shuffledList
remove from originalList at index
}
[PseudoCode]
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1