0

Already working from last 10 hours couldn't find the working solution.

If anyone could help, this will be really peaceful!

This is my source code =

@foreach (var datas in data)
{                   
    <a class="mylink" href="#" target="_blank">@datas.text</a>                   
}

@code { 
    private IEnumerable<RandomPick> data = new[] {
        new RandomPick { id = 1, text = "Diamond Price" },
        new RandomPick { id = 2, text = "Gold Price" },
        new RandomPick { id = 3, text = "Web Hosting"},
        new RandomPick { id = 4, text = "Insurance Price",
    };
}

My requirement is that, here our of 4 objects I want to choose any 2 random element only on the foreach loop, so when the loop is executed I only want to select 2 random element from the 4 object elements.

Dimitris Maragkos
  • 8,932
  • 2
  • 8
  • 26
Arjun Ghimire
  • 301
  • 1
  • 2
  • 8

1 Answers1

1

What you are looking for is how to pick two random numbers out of four possible numbers without duplicates. And the answer is:

Generate a sequence between 0 and 3 (data.Count() - 1), shuffle them and pick 2 number out of the sequence.

@foreach (var number in randomNumbers)
{                   
    <a class="mylink" href="#" target="_blank">@data.ElementAt(number).text</a>                   
}

@code {
    private IEnumerable<RandomPick> data = new[] {
        new RandomPick { id = 1, text = "Diamond Price" },
        new RandomPick { id = 2, text = "Gold Price" },
        new RandomPick { id = 3, text = "Web Hosting"},
        new RandomPick { id = 4, text = "Insurance Price",
    };

    private Random random = new();

    private IEnumerable<int> randomNumbers = Enumerable.Empty<int>();

    protected override void OnInitialized()
    {
        randomNumbers = Enumerable.Range(0, data.Count() - 1) // { 0, 1, 2, 3 } generate sequence
            .OrderBy(x => random.Next())                      // { 3, 1, 0, 2 } random shuffle
            .Take(2)                                          // { 3, 1 }       pick two
            .ToList();
    }
}

Source for picking the random numbers: https://stackoverflow.com/a/26931594/10839134

Dimitris Maragkos
  • 8,932
  • 2
  • 8
  • 26
  • You are awesome, thank you very much brother, but there is a problem. When its applied then on the display it changes the postition up to down, you can view this screenshot to understand what I mean = https://prnt.sc/uH2fX8-R0bwI – Arjun Ghimire Aug 29 '22 at 18:59
  • So what I mean is, it keeps changing value and then gets stable after around 10-15 seconds. – Arjun Ghimire Aug 29 '22 at 19:01
  • 1
    I can't reproduce the issue. Could you edit your question and write what you tried? Also I noticed that `@data[number].text` was giving error "cannot apply indexing to IEnumerable" so I changed to `@data.ElementAt(number).text`. – Dimitris Maragkos Aug 29 '22 at 19:44
  • I have uploaded the website to the live environment, Please go here - https://dlupload.com/FileDetail/2085022806 now click on free download button and click on explore topic first button then you will see that interface. and regarding that enumerable, I did similar thing. Currently its automatically changing the value of the buttons. Or is it possible if we can delay the time of its changing? currnently its changing extremly fast? Can you do something to make it slower? Please this will be so great! – Arjun Ghimire Aug 29 '22 at 19:50
  • BlazorTimer timer; private int secondsLeft=0; private void InitializeDelay() { secondsLeft = delayState.info.delay; timer = new(1000, delayState.info.delay); } On my website I have this line of code, is it causing that prorblem? – Arjun Ghimire Aug 29 '22 at 19:55
  • 1
    Yes, it's because I made `randomNumbers` a calculated property so every time your timer ticks `StateHaChanged` gets called and it generates new numbers. The solution is to make `randomNumbers` a field and populate it only once inside `OnInitialized()`. I updated my answer. – Dimitris Maragkos Aug 29 '22 at 20:14