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.

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

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 = prnt.sc/uH2fX8-R0bwI
So what I mean is, it keeps changing value and then gets stable after around 10-15 seconds.
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.
I have uploaded the website to the live environment, Please go here - 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!
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.