0

So here is the basic code:

    namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        protected Random random;

        public Form1()
        {
            InitializeComponent();
            random = new Random();
        }

        private void Form1_Load(object sender, EventArgs e)
        { }

        private void button1_Click(object sender, EventArgs e)
        {
            bool button1Clicked = true;
            if (button1Clicked == true) { ITIpanel.Visible = true; }
        }

        private void ITIpanel_Paint(object sender, PaintEventArgs e)
        {
            ITItimer.Enabled = true;
        }

        private void ITItimer_Tick(object sender, EventArgs e)
        {
            double rand = random.NextDouble();
            if (rand < .50d) { bluestimPanel.Visible = true; }
            else if (rand > .5d) { redstimPanel.Visible = true; }

            ITItimer.Enabled = false;
        }

        private void bluestimPanel_Paint(object sender, PaintEventArgs e)
        {
            Trialtimer.Enabled = true;
        }

        private void redstimPanel_Paint(object sender, PaintEventArgs e)
        {
            Trialtimer.Enabled = true;
        }

        private void Trialtimer_Tick(object sender, EventArgs e)
        {
            bluestimPanel.Visible = false;
            redstimPanel.Visible = false;
            Trialtimer.Enabled = false;
            ITIpanel.Visible = true;
        }
    }
}

As you can see, the program itself is fairly straight forward. At the tick of the ITItimer the red or blue panels occurs at random. I want to modify this such that if the ITItimer ticks a total of 10 times, the red and blue panels will both have occurred 5 times each.

I have been researching this for a week or so and have yet to find a solution. Any ideas on how I could best accomplish this?

I actually got the following to work:

double rand = random.NextDouble();
if (rand < .50d && blue < 5) { bluestimPanel.Visible = true; }
else if (blue == 5) { redstimPanel.Visible = true; }
if (rand > .5d && red < 5) { redstimPanel.Visible = true; }
else if (red == 5) { bluestimPanel.Visible = true; }
if (red >= 5 && blue >= 5) { panel1.Visible = true; } 

It isn't exactly the prettiest thing in the world. But it gets the job done.

cwdaniels
  • 71
  • 1
  • 7
  • If your requirement is that they must each appear exactly five times then you shouldn't be using random numbers (at least not entirely). Although it is statistically likely over a very large set that exactly half will be red and half blue, that's not guaranteed (especially with most out-of-the-box random number generators) and certainly not for such a small sample. – Michael Todd May 14 '12 at 22:16
  • 5
    Sounds like you want to randomize a set of results in your click event and pull from them as the timer ticks? Consider it a deck of 10 cards and apply a shuffling algorithm. – Tetsujin no Oni May 14 '12 at 22:18
  • @TetsujinnoOni Something like that. Except in this case, say you have two possible trial types and a session consist of 10 trials. I want to present the two types at random throughout those 10 trials such that every session starts and ends differently. I'm fairly new to computer programming, so any help would be appreciated. – cwdaniels May 14 '12 at 22:22
  • Also, I'm thinking an array of sorts that lists the possible trial types as well as something that calls the different trial types such that they can each only occur 5 times. – cwdaniels May 14 '12 at 22:29
  • Do you have multiple trial types as a possibility, with multiple being more than two? Is a session only 10 trials, or is it up to 5 trials with a desirable evenness among trial types? – Tetsujin no Oni May 14 '12 at 22:55

1 Answers1

2

Random numbers using most normal library routines are a low-quality source of pseudorandomness. If this is for a randomized scientific study, this will be a flaw in your protocol design.

The approach that I would recommend would be to consider this a method of randomly arranging a session of at least N trials, where there are X trial types.

The below is pseudocode for illustration of the concept.

Let MinimumTrials be N MOD X + X
Let SessionList be a List<Trial>
For Each TrialType 
    add X instances of that trial type to SessionList
Shuffle(SessionList)

Then your session engine can call the individual Trials as it walks through the SessionList to have an even distribution of possible trial orders. Note that Shuffle is an operation which requires a certain degree of finesse to get right, searching on SO is a good starting point for that.

Tetsujin no Oni
  • 7,300
  • 2
  • 29
  • 46
  • Adding to Tetsujin's answer this [SO question](http://stackoverflow.com/questions/375351/most-efficient-way-to-randomly-sort-shuffle-a-list-of-integers-in-c-sharp) discusses good shuffling algorithms. – Thomas C. G. de Vilhena May 14 '12 at 23:21
  • This is exactly what I was looking for! Very similar to what the grad students use in MED PC notation. – cwdaniels May 14 '12 at 23:59