Home » Visual StudioRSS

Permuted Block Randomization

Hi,

I am trying to develop permuted block randomization using C#/VB.

I don't know where to start. Can anyone help me to start, please?

Thanks

 

11 Answers Found

 

Answer 1

I recently answered this. Will post some code.

 

Answer 2

Here is the class to do the randomisation:

// <copyright file="RandomTrial.cs" company="Cive Chinery">
// Copyright LGPL (c) 2010 All Right Reserved
// </copyright>

namespace Demo
{
  using System;
  using System.Collections.ObjectModel;

  /// <summary>
  /// Random Trial
  /// </summary>
  public class RandomTrial
  {
    /// <summary>
    /// treatment Master
    /// </summary>
    private readonly Collection<string> treatmentMaster;

    /// <summary>
    /// Make random available within a static library
    /// </summary>
    private static readonly Random randomSeed = new Random();

    #region constructors
    /// <summary>
    /// Initializes a new instance of the <see cref="RandomTrial"/> class.
    /// </summary>
    /// <param name="treatment">The treatment.</param>
    public RandomTrial(Collection<string> treatment)
    {
      if (treatment == null)
      {
        throw new ArgumentNullException("treatment");
      }

      this.treatmentMaster = treatment;
      if (this.MasterCount == 0)
      {
        throw new ArithmeticException("At least treatment must be present in the collection");
      }
    }

    /// <summary>
    /// Prevents a default instance of the <see cref="RandomTrial"/> class from being created.
    /// </summary>
    private RandomTrial()
    {
    }
    #endregion
    #region Properties
    /// <summary>
    /// Gets the master count.
    /// </summary>
    /// <value>The master count.</value>
    public int MasterCount
    {
      get
      {
        return this.treatmentMaster.Count;
      }
    }

    /// <summary>
    /// Gets the treatment master.
    /// </summary>
    /// <value>The treatment master.</value>
    public Collection<string> TreatmentMaster
    {
      get
      {
        return this.treatmentMaster;
      }
    }

    /// <summary>
    /// Gets the random block.
    /// </summary>
    /// <value>The random block.</value>
    public Collection<string> RandomBlock
    {
      get
      {
        var outputBlock = new Collection<string>();
        var copyBlock = new Collection<string>();
        foreach (var item in this.treatmentMaster)
        {
          copyBlock.Add(item);   
        }

        while (copyBlock.Count > 1)
        {
          int next = randomSeed.Next() % copyBlock.Count;
          outputBlock.Add(copyBlock[next]);
          copyBlock.RemoveAt(next);
        }

        outputBlock.Add(copyBlock[0]);
        return outputBlock;
      }
    }
    #endregion
  }
}

 

Answer 3

Here is the unit test class that I used:

// <copyright file="RandomTrialFixture.cs" company="Cive Chinery">
// Copyright LGPL (c) 2010 All Right Reserved
// </copyright>

namespace Demo
{
  using System;
  using System.Collections.ObjectModel;
  using System.Text;
  using NUnit.Framework;

  /// <summary>
  /// Random Trial
  /// </summary>
  [TestFixture]
  public class RandomTrialFixture
  {
    #region TestNull...
    /// <summary>
    /// Tests the null 1.
    /// </summary>
    [Test]
    public void TestNull1()
    {
      try
      {
        Collection<string> treatmentMaster = null;
        var randomTrial = new RandomTrial(treatmentMaster);
        Assert.Fail();
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.ToString());
      }
    }

    /// <summary>
    /// Tests the null 2.
    /// </summary>
    [Test]
    public void TestNull2()
    {
      try
      {
        var randomTrial = new RandomTrial(null);
        Assert.Fail();
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.ToString());
      }
    }
    #endregion
    #region TestOne
    /// <summary>
    /// Tests with a collection of one.
    /// </summary>
    [Test]
    public void TestOne()
    {
      try
      {
        var treatmentMaster = new Collection<string> { "T1" };
        var randomTrial = new RandomTrial(treatmentMaster);
        Assert.Fail();
      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.ToString());
      }
    }
    #endregion
    #region TestSix
    /// <summary>
    /// Tests with a collection of six.
    /// </summary>
    [Test]
    public void TestSix()
    {
      var treatmentMaster = new Collection<string> { "T1", "T2", "T3", "T4", "T5", "T6" };
      var randomTrial = new RandomTrial(treatmentMaster);
      Assert.AreEqual(6, randomTrial.MasterCount);
      var results = randomTrial.RandomBlock;
      var display = new StringBuilder(50);
      var addComma = false;
      foreach (var result in results)
      {
        if (addComma)
        {
          display.Append(",");  
        }

        display.Append(result);
        addComma = true;
      }

      Console.WriteLine(display);
      display = new StringBuilder(50);
      results = randomTrial.RandomBlock;
      addComma = false;
      foreach (var result in results)
      {
        if (addComma)
        {
          display.Append(",");
        }

        display.Append(result);
        addComma = true;
      }

      Console.WriteLine(display);
      display = new StringBuilder(50);
      results = randomTrial.RandomBlock;
      addComma = false;
      foreach (var result in results)
      {
        if (addComma)
        {
          display.Append(",");
        }

        display.Append(result);
        addComma = true;
      }

      Console.WriteLine(display);
    }
    #endregion
  }
}

 

Answer 4

A typical test result was:

T2,T3,T1,T6,T5,T4
T4,T1,T6,T3,T2,T5
T5,T4,T6,T3,T1,T2

 

Answer 5

        public void TestSix()
        {
            var treatmentMaster = new Collection<string> { "T1", "T2", "T3", "T4", "T5", "T6" };
            var randomTrial = new RandomTrial(treatmentMaster);
            Assert.AreEqual(6, randomTrial.MasterCount);
            var results = randomTrial.RandomBlock;
            var display = new StringBuilder(50);
            var addComma = false;
            foreach (var result in results)
            {
                if (addComma)
                {
                    display.Append(",");
                }
                display.Append(result);
                addComma = true;
            }
            Console.WriteLine(display);
            display = new StringBuilder(50);
            results = randomTrial.RandomBlock;
            addComma = false;
            foreach (var result in results)
            {
                if (addComma)
                {
                    display.Append(",");
                }
                display.Append(result);
                addComma = true;
            }
            Console.WriteLine(display);
            display = new StringBuilder(50);
            results = randomTrial.RandomBlock;
            addComma = false;
            foreach (var result in results)
            {
                if (addComma)
                {
                    display.Append(",");
                }
                display.Append(result);
                addComma = true;
            }
            Console.WriteLine(display);
        }

Thanks very much TATWRTH for your help.

I am very sorry for getting back late.

I am developing a web application in asp.net.

So, I copied RandomTrial class to my App_Code folder, then I copied the content of TestSix() to my Default.aspx page with a modification of 

 Console.WriteLine(display); to           Response.Write(display);

and when I run the application the system will generate six treatments like this:-

T2,T3,T5,T1,T4,T6


Thanks,

 

 

Answer 6

Hi Tatwrth,

How do you generate random numbers? Is it using the Random Class within C#?

Is it possible for me to set the seed?

Thanks

 

Answer 7

When I next get to my home development box, I will need to try out

new Random(seedValue) instead of new Random()

 

Answer 8

Thanks TATWORTH.


 

Answer 9

TATWORTH:
new Random(seedValue) instead of new Random()

seedValue may be an integer

 

Answer 10

Thanks very much TATWORTH.

 

Answer 11

Hi, 

Has anybody got any clue about implementing minimization method in randomizaion?

Thanks

 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter