0

I'm trying to exclude old results from a random number generator but I can't seem to get the variables to remember past two. I'm new to C# so there's probably some really simple solution I'm overlooking. It'd be super helpful if you guys could enlighten me.

I've already tried the code shown below, and I'm fairly sure I'm being stupid but I can't figure out why it's not working.

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

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label_click(object sender, EventArgs e)
        {
            Close();
        }

        int mouseX = 0, mouseY = 0;
        bool mouseDown;



        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            mouseDown = true;
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            mouseDown = false;
        }

        private void GOBUTN_Paint(object sender, PaintEventArgs e)
        {



        }
        //Sharing is caring: Communism goes here
        private System.Windows.Forms.Timer timtim;
        PictureBox Rooskie = new PictureBox();
        int duplicheck = 0;
        int duplicheck2 = 0;
        int duplicheck3 = 0;
        Label test = new Label();
        private void boopthesnoot(object sender, EventArgs e)
        {
            dinging:
            //yeah, random numbers here
            Random rando = new Random();
            int rand0 = rando.Next(1, 25);
            test.Text = duplicheck + ", " + duplicheck2 + ", " + duplicheck3;
            test.Font = new Font("Calibri", 20);
            Controls.Add(test);
            test.Location = new Point(0, 200);
            test.Height = 1000;
            test.Width = 1000;
            if(duplicheck != rand0 && duplicheck2 != rand0 && duplicheck3 != rand0)
            {
                GOBUTTON.Hide();
                pictureBox1.Hide();
                pictureBox2.Hide();

                //image code goes here
                string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + "resources" + "\\" + rand0 + ".jpg";
                Rooskie.Width = 1160;
                Rooskie.Height = 620;
                Bitmap image = new Bitmap(filepath);
                Rooskie.Dock = DockStyle.Fill;
                Rooskie.Image = (Image)image;
                Controls.Add(Rooskie);
                Rooskie.SizeMode = PictureBoxSizeMode.CenterImage;


                //Aww, it's that timer time
                timtim = new System.Windows.Forms.Timer();
                timtim.Tick += new EventHandler(clockfinish);
                timtim.Interval = 5000;
                timtim.Start();
            }
            else
            {
                goto dinging;
            }

            if (duplicheck != rand0)
            {
                duplicheck2 = duplicheck;
            }

            if(duplicheck != rand0)
            {
                duplicheck2 = duplicheck;
            }

            if (duplicheck2 != duplicheck)
            {
                duplicheck3 = duplicheck2;
            }

            duplicheck = rand0;

        }

        private void clockfinish(object sender, EventArgs e)
        {
            //CEASE THE TIMER AND GIVE ME BACK MY BUTTON
            Rooskie.Image = null;
            timtim.Stop();
            GOBUTTON.Show();
            pictureBox1.Show();
            pictureBox2.Show();
        }

The idea is that a random number generator picks an image and displays it. But I don't want the same image to show up multiple times in a row so this is supposed to give it a buffer i.e. If image 2 was displayed on the first button press, image 1 was displayed on the second, and image 3 was displayed on the third, the results should look like: duplicheck = 2 duplicheck2 = 1 duplicheck3 = 3

tripleee
  • 175,061
  • 34
  • 275
  • 318
TheWalrus
  • 11
  • 4
  • Have you debugged this with the step debugger? – TheGeneral Jul 26 '19 at 06:58
  • The code compiles and runs just fine. It just doesn't save the numbers the way it's supposed to. I wind up with outputs like 1, 17, 17 or 1, 17, 0 depending on how I tweak it. – TheWalrus Jul 26 '19 at 07:02
  • he means to step through it and see where it is going wrong. But, if I were doing this I would store my image paths in a list. Select a random element between 0 and the count of the list, when it is selected remove it from the list and store it in a variable, on the next image change (it cant be a duplicate), add the image back into the list and remove the newly selected one – Kevin Jul 26 '19 at 07:05
  • _If_ you're generating random numbers within finite bounds, you should use a shuffle (Fisher-Yates is common here) on a pre-generated list instead of checking an ever-growing list for collisions. – ProgrammingLlama Jul 26 '19 at 07:15
  • @John I'm not super familiar with that method. Is it using an array instead of individual variables? – TheWalrus Jul 26 '19 at 08:16
  • Yes. Say you want numbers between 1 and 10 without repeating, you create a 10-item list with those values. You then shuffle the list. Now, going through the list in index order (`l[0]` to `l[9]`) will give you your numbers in a random order. See [this question](https://stackoverflow.com/questions/273313/randomize-a-listt) for more info. – ProgrammingLlama Jul 26 '19 at 08:19
  • Thanks, I'll definitely give this a try – TheWalrus Jul 26 '19 at 08:45

1 Answers1

0

So with the help of everyone in the comments and a lot of messing around I wound up with this. It works perfectly to prevent any image from being displayed more than once per 25 clicks of the button. Thank you guys so much for the help on this! I don't think I'd have figured this out without you.

//Sharing is caring: Communism goes here
        private System.Windows.Forms.Timer timtim;
        int rand0;
        PictureBox Rooskie = new PictureBox();
        Label test = new Label();
        Random rando = new Random();
        List<int> duplicheck = new List<int>();


        private void boopthesnoot(object sender, EventArgs e)
        {
        dingding:
            //yeah, random numbers here
            rand0 = rando.Next(1, 26);


            /*string combowombo = string.Join(", ", duplicheck.ToArray());
            test.Text = combowombo;
            test.Font = new Font("Calibri", 20);
            Controls.Add(test);
            test.Location = new Point(0, 200);
            test.Height = 1000;
            test.Width = 1000;*/
            if(duplicheck.Contains(rand0))
            {
                goto dingding;
            }
            else
            {
                GOBUTTON.Hide();
                pictureBox1.Hide();
                pictureBox2.Hide();

                //image code goes here
                Rooskie.Width = 1160;
                Rooskie.Height = 620;
                Bitmap image = new Bitmap(WindowsFormsApp1.Properties.Resources._1);
                Rooskie.Dock = DockStyle.Fill;
                Rooskie.Image = (Image)image;
                Controls.Add(Rooskie);
                Rooskie.SizeMode = PictureBoxSizeMode.CenterImage;


                //Aww, it's that timer time
                timtim = new System.Windows.Forms.Timer();
                timtim.Tick += new EventHandler(clockfinish);
                timtim.Interval = 3000;
                timtim.Start();
                duplicheck.Add(rand0);
                if (duplicheck.Count == 25)
                {
                    duplicheck = new List<int>();
                }
            }
        }
tripleee
  • 175,061
  • 34
  • 275
  • 318
TheWalrus
  • 11
  • 4