【问题标题】:Generate random numbers, that won't show up again, unknown number pool生成随机数,不会再出现,未知数池
【发布时间】:2014-01-31 10:20:54
【问题描述】:

你好,我写了一个程序,显示文件夹中的所有图片,.exe 在。 它包含一个按钮,该按钮“随机”选择另一张图片并显示。 到目前为止效果很好。

接下来我想做的是,按钮选择一张以前没有选择过的图片。并且只有在显示每张图片时,程序才会重置并重新开始显示所有图片。

但是当我现在启动程序时,它会显示一张图片,仅此而已。该按钮没有做任何可见的事情,您无法更改第一张图片 - 我不知道为什么。 (我看到了类似的问题,比如二十一点主题。但就我而言,我不知道会有多少张图片,所以我无法创建一个随机数的固定池并将它们从中删除列表或类似的东西)。

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace randompix
{
    public partial class Form1 : Form
    {

        int i = 0;
        Random rnd = new Random();
        string pfad = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
        IEnumerable<string> allebilder; // Will contain every filename of the folder
        List<int> wdhlist = new List<int>(); //will contain 'i' that was already used



        public Form1()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
            pBox1.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top);
            pBox1.SizeMode = PictureBoxSizeMode.Zoom;
            btn2.Anchor = (AnchorStyles.Top | AnchorStyles.Right);

            allebilder = Directory.EnumerateFiles(pfad); 
            wdhlist.Add(i);

            if (allebilder.ElementAt<string>(i).EndsWith(".jpg") || allebilder.ElementAt<string>(i).EndsWith(".jpeg") || allebilder.ElementAt<string>(i).EndsWith(".png") || allebilder.ElementAt<string>(i).EndsWith(".gif") || allebilder.ElementAt<string>(i).EndsWith(".bmp"))
            {
                pBox1.ImageLocation = allebilder.ElementAt<string>(i);
                label1.Text = allebilder.ElementAt<string>(i);
            }

            else 
            {
                if (i == 0) // because 0 is already added 
                {
                    i = rnd.Next(allebilder.Count<string>());
                }
                else
                {
                    wdhlist.Add(i);
                    i = rnd.Next(allebilder.Count<string>());
                }
            }
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            i = rnd.Next(allebilder.Count<string>());
            wdhlist.Add(i);

            if (wdhlist.Count() >= allebilder.Count<string>())
            {
                wdhlist.Clear();
            }

            else
            {
                if (wdhlist.Contains(i))
                {
                    i = rnd.Next(allebilder.Count<string>());
                    wdhlist.Add(i);
                }

                else
                {
                    if (allebilder.ElementAt<string>(i).EndsWith(".jpg") || allebilder.ElementAt<string>(i).EndsWith(".jpeg") || allebilder.ElementAt<string>(i).EndsWith(".png") || allebilder.ElementAt<string>(i).EndsWith(".gif") || allebilder.ElementAt<string>(i).EndsWith(".bmp"))
                    {
                        if (allebilder.ElementAt<string>(i) == null)
                        {
                            MessageBox.Show("Nope");
                        }

                        else
                        {
                            label1.Text = allebilder.ElementAt<string>(i);
                            pBox1.ImageLocation = allebilder.ElementAt<string>(i);
                        }
                    }
                    else
                    {
                        i = rnd.Next(allebilder.Count<string>());
                        wdhlist.Add(i);
                    }
                }
            }
        }

        private void btn1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btn1_Click(sender, e);
            }
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Version 0.3\n\nUse: Copy .exe in a folder with pictures\nSupports .jpg, .gif, .png and .bmp\n\nProgram by Fabian Schmidt\nIcon by Aleksandra Wolska\n(https://www.iconfinder.com/iconsets/49handdrawing)");
            btn1.Focus();
        }
    }
}

【问题讨论】:

    标签: c# random numbers repeat


    【解决方案1】:

    您需要形成一个图像文件名列表。然后形成一个索引列表 - 从 0 到列表中该图像的数量。

    List<int> numberList = Enumerable.Range(0, imagesList.Count).ToList();
    

    然后你得到从 0 到那个 numberList.Count 的随机数

    int randomValue = numberList[rnd.Next(0, numberList.Count)];
    

    并且在你使用它之后,将它从 numberList 中移除

    DoSomeThingWithImage(imagesList[randomValue]);
    numberList.Remove(randomValue);
    

    因此,您将拥有不重复的随机图像,直到它们都没有出现。然后再做一遍。 希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      您可以按随机顺序创建图像序列,然后在完全使用列表时重新创建序列

      private Random rnd = new Random();
      private IEnumerable<string> GetImagesInRandomOrder(){
          return Directory.EnumerateFiles(pfad).OrderBy(x => rnd.Next());
      }
      
      private IEnumerator<string> randomImages = 
                Enumerable.Empty<string>.GetEnumerator();
      
      private string GetNextImage(){
             if(!randomImages.MoveNext()){
                randomImages = GetImagesInRandomOrder().GetEnumerator();
                if(!randomImages.MoveNext()){ 
                    throw new InvalidOperationException("No images"); 
                }   
             }
             return randomImages.Current;
      }
      

      然后你会像这样使用它:

      DoSomeThingWithImage(GetNextImage());
      

      【讨论】:

        猜你喜欢
        • 2015-03-14
        • 1970-01-01
        • 1970-01-01
        • 2016-10-08
        • 1970-01-01
        • 2018-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多