-2

I was making a loop that picks the correct 9th element of an array randomly each time it loops through.

There are 99 elements in my loop in total.

Each 9th element will be the same( % 9 ). I need to make the rest 90 of them loop randomly each time i open the application.

Check my code:

    public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        string[] assignments = new string[] { "https://cdn2.iconfinder.com/data/icons/animals/48/Turtle.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Butterfly.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Dolphin.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Elephant.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Hippopotamus.png", "https://cdn2.iconfinder.com/data/icons/animals/48/Panda.png" };
        Random rnd = new Random();

        string[] randomingArray = assignments.OrderBy(x => rnd.Next()).ToArray();

        string repeatNumber = "";

        List<string> animals = new List<string>();

        for (int i = 1; i < 100; i++)
        {
            if (i == 9)
            {
                repeatNumber = randomingArray[i % randomingArray.Length];
                animals.Add(repeatNumber);
            }
            else if ((i % 9) == 0)
            {
                animals.Add(repeatNumber);
            }
            else
            {
                // random animals.Add(); <-- it should loop randomly here.
            }
            ItemsControl1.ItemsSource = animals;
        }
    }
}

After that i add my elements to a list and send that list to xaml via binding.

Ralfs R
  • 77
  • 1
  • 8
  • 1
    Don't have time to write the code, but the typical solution to this problem is to copy your 90 remaining "random" items to a different array or list and then [_shuffle_](https://stackoverflow.com/questions/273313/randomize-a-listt) the list. – Joel Coehoorn Nov 08 '17 at 20:27
  • *Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking.* – L.B Nov 08 '17 at 20:51
  • For shuffling, see https://stackoverflow.com/questions/1651619/optimal-linq-query-to-get-a-random-sub-collection-shuffle/1653204#1653204 – Hans Kesting Nov 08 '17 at 20:52
  • @HansKesting - The OP's approach to shuffling is perfectly fine. – Enigmativity Nov 08 '17 at 22:44

2 Answers2

1

I think this will give you the results you want:

private Random rnd = new Random();
public MainWindow()
{
    InitializeComponent();
    string[] assignments = new string[]
    {
        "https://cdn2.iconfinder.com/data/icons/animals/48/Turtle.png",
        "https://cdn2.iconfinder.com/data/icons/animals/48/Butterfly.png",
        "https://cdn2.iconfinder.com/data/icons/animals/48/Dolphin.png",
        "https://cdn2.iconfinder.com/data/icons/animals/48/Elephant.png",
        "https://cdn2.iconfinder.com/data/icons/animals/48/Hippopotamus.png",
        "https://cdn2.iconfinder.com/data/icons/animals/48/Panda.png"
    }.OrderBy(x => rnd.Next()).ToArray();

    string[] animals =
        Enumerable
            .Range(0, 99)
            .Select(i => assignments[i % assignments.Length])
            .ToArray();

    foreach (int i in Enumerable.Range(1, 9))
    {
        animals[i * 9] = assignments[9 % assignments.Length];
    }

    ItemsControl1.ItemsSource = animals;
}

Just a small hint to save you trouble, always make your Random variable a single field - this will avoid potential mistakes with rapidly call code repeating random numbers. That won't happen in this code, but it's just a good habit to get in to.


I just realised that I could lose the foreach loop by doing this:

    string[] animals =
        Enumerable
            .Range(0, 99)
            .Select(i => assignments[i % 9 == 0 ? 9 : i % assignments.Length])
            .ToArray();

That's even better.

Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • Hello! Thank you for your answer. This line gives me an error when compiling .Select(i => assignments[i]) "System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'" How can i fix that? – Ralfs R Nov 09 '17 at 17:17
  • @RalfsR - Sorry, I made a small mistake. I fixed it. – Enigmativity Nov 09 '17 at 20:56
0

you could do something like

var rand = new Random();
int value = rand.Next(0, 99); // 0 is the min and 99 is the max

And to get a random element in an array you would just do

var result = randomingArray[value];  

If you need to check to see if the number was 9 you could do

if(value == 9)

{

}

If you just need to pick the 9th element each time you could just do

var result = randomingArray[9];  
bird
  • 152
  • 10