【问题标题】:Randomize a list but with range随机化列表但有范围
【发布时间】:2017-06-22 09:34:22
【问题描述】:

我有一个包含72 项目的列表。该程序有点像Tinder,其中显示了图片和一些文字。

我希望这个List 是随机的,但不是第一个“卡片”和最后一个“卡片”,例如。 item no. 1 & item no. 72必须保留为第一张和最后一张卡片,其余70项将随机排序。

这是我定义列表的sn-p代码

public class MainPageViewModel : INotifyPropertyChanged
{
   public event PropertyChangedEventHandler PropertyChanged;

   List<CardStackView.Item> items = new List<CardStackView.Item>();
   public List<CardStackView.Item> ItemsList
   {
      get { return items; }
      set { if (items == value) { return; } items = value; OnPropertyChanged(); }
   }

   protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
   {
      PropertyChangedEventHandler handler = PropertyChanged;
      if(handler != null)
      {
         handler(this, new PropertyChangedEventArgs(propertyName));
      }
   }

   protected virtual void SetProperty<T>(ref T field, T value, [CallerMemberName] string propertyName = null)
   {
      field = value;
      PropertyChangedEventHandler handler = PropertyChanged;
      if(handler != null)
      {
         handler(this, new PropertyChangedEventArgs(propertyName));
      }
   }

   public MainPageViewModel()
   {
      items.Add(new CardStackView.Item() { Name = "xxxx", Photo = new Uri("yyyy"), Description = "zzzz", ID = 1 });
      items.Add(new CardStackView.Item() { Name = "xxxx", Photo = new Uri("yyyy"), Description = "zzzz", ID = 2 });
      items.Add ........
      items.Add(new CardStackView.Item() { Name = "xxxx", Photo = new Uri("yyyy"), Description = "zzzz", ID = 72 });
   }
}

我可以在它们都在一个列表中时这样做,还是应该创建一个多维数组。其中索引 1 是 item no. 1,索引 2 是 Randomized List,索引 3 是 item no. 72。如果这是一个合适的解决方案,我将如何在我的卡片上显示它们。

我一直在研究类似Randomize a List<T>Best way to randomize an array with .NET 的问题,但没有成功。

【问题讨论】:

  • 您正在制作列表,那么为什么不列出您想要随机播放的项目,然后根据您已经链接到的问题的答案将它们随机播放,然后当您完成时改组将您的静态 Item#1 和 Item#72 添加到适当的位置?
  • @Mashton 哦,是的,为什么我没有想到那个 facepalm,我会试试的 :)

标签: c# list xamarin random xamarin.forms


【解决方案1】:

a standard random shuffle algorithm 接受起始索引和计数非常容易:

public static void Shuffle<T>(IList<T> array, Random rng, int first, int count)
{
    for (int n = count; n > 1;)
    {
        int k = rng.Next(n);
        --n;
        T temp = array[n+first];
        array[n + first] = array[k + first];
        array[k + first] = temp;
    }
}

那么如果你想洗牌除了第一个和最后一个项目:

Shuffle(items, new Random(), 1, items.Count-2);

【讨论】:

    【解决方案2】:

    试试这个:

    private Random random = new Random();
    
    public MainPageViewModel()
    {
        /* Populate `items` */
    
        items =
            items
                .Take(1)
                .Concat(items.Skip(1).Take(items.Count() - 2).OrderBy(x => random.Next()))
                .Concat(items.Skip(items.Count() - 1))
                .ToList();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      • 2015-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-06
      • 1970-01-01
      相关资源
      最近更新 更多