Some code not fully tested apart from the shuffle method.
var nodes = Model.Children.ToList();
var random = nodes.Shuffle().Take(9);
public static void Shuffle<T>(this IList<T> list)
{
var provider = new RNGCryptoServiceProvider();
int n = list.Count;
while (n > 1)
{
var box = new byte[1];
do provider.GetBytes(box);
while (!(box[0] < n * (Byte.MaxValue / n)));
int k = (box[0] % n);
n--;
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
Extension method must be defined in a non-generic static class,
and if i dont use @using System.Collections; i have error
Using the generic type 'System.Collections.Generic.IList' requires 1 type arguments
@using System.Collections;
@functions{
public static void Shuffle(this IList list)
{
var provider = new RNGCryptoServiceProvider();
int n = list.Count;
while (n > 1)
{
var box = new byte[1];
do provider.GetBytes(box);
while (!(box[0] < n * (Byte.MaxValue / n)));
int k = (box[0] % n);
n--;
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
you can either create static class and put the method in there or update the metho signature and get rid of this keyword they call it like Shuffle(yourlist)
9 Random items
Hello, please i need help ...
I have about 50 items in Model.Children, and i want to get 9 random items that will show to homepage.
Every time when homepage is reloaded, i need to show different items,
Please, how can i do that ?
Denis,
Some code not fully tested apart from the shuffle method.
i have try but with problems ...
Extension method must be defined in a non-generic static class,
and if i dont use @using System.Collections; i have error Using the generic type 'System.Collections.Generic.IList' requires 1 type arguments
@using System.Collections; @functions{
}
denis,
you can either create static class and put the method in there or update the metho signature and get rid of this keyword they call it like Shuffle(yourlist)
Regards
Ismail
Thank You, i will try ...
is working on a reply...