Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   populate array with random nonrepeating #'s (http://www.programmingforums.org/showthread.php?t=15729)

ghg5 Apr 29th, 2008 10:06 AM

populate array with random nonrepeating #'s
 
I am trying to fill an array with random #'s from 1 to 42 using every number in the range exactly once. here is what I have:
:

  1. private void frmMain_Load(object sender, System.EventArgs e)
  2.                 {
  3.                         //seed the random number function
  4.                         DateTime dtmCurrent = DateTime.Now;
  5.                         generateRandom = new Random(dtmCurrent.Millisecond);
  6.  
  7.                         //fill array with random #'s from 1 to 42
  8.                         int intAdd = 0;
  9.                         while(intAdd < 42)
  10.                         {
  11.                                 intStore = generateRandom.Next(1,42);
  12.                                 //check to see if array contains generated #
  13.                                 foreach(int intBoard1 in intBoard)
  14.                                 {
  15.                                         if(intStore == intBoard1)
  16.                                         {
  17.                                                 bln_isSame = true;
  18.                                         }
  19.                                         else
  20.                                         {
  21.                                                 bln_isSame = false;
  22.                                         }
  23.                                 }
  24.                                 //if array doesn't contain generated number then add it to the array in next position
  25.                                 if(bln_isSame == false)
  26.                                 {
  27.                                         intBoard[intAdd] = intStore;
  28.                                         intAdd++;
  29.                                 }
  30.                         }
  31.                         int intIndex=0;
  32.                         //write array to a file for test
  33.                         StreamWriter arrayStreamWriter = new StreamWriter("C:\\Documents and Settings\\Owner.Gir\\My Documents\\Visual Studio Projects\\Memory\\array.txt",true);
  34.                         while(intIndex < 42)
  35.                         {
  36.                                 arrayStreamWriter.WriteLine(intBoard[intIndex]);
  37.                                 intIndex++;
  38.                         }
  39.                         arrayStreamWriter.Close();
  40.                 }

a few of the variables are declared at the class level so I didn't show their declaration. This code seems to compile but I get a list that has repeats in it when I print the array to a file. Is there a shuffle method or something super obvious that I am missing? I am using VS 2003 for this project.

Freaky Chris Apr 29th, 2008 11:53 AM

Re: populate array with random nonrepeating #'s
 
Should it not just be as simple as checking storing the last number to be generated and stored, then checking it agaisnt the new number to see if it is the same if it is then generate a new number?

Sorry, re-read you may want to store each randomly generated number in an array then check each new number against those in the array.

im confusing myself lol

Chris

ghg5 Apr 29th, 2008 2:21 PM

Re: populate array with random nonrepeating #'s
 
thank u...storing the random numbers in an array first seemed to do what i needed, however i am still getting a 0 printed at the end of my list. Any ideas on why i get this and how to get rid of it?

Alias Apr 29th, 2008 2:26 PM

Re: populate array with random nonrepeating #'s
 
You want to fill an array with a length of fourty-two, with numbers one to fourty-two and further yet the numbers should be at random points in the array, right?

If I got that right, why not just fill the array with values one through to fourty-two and manually shuffle the array?

Anyway, continuing with your own technique, this ought to help:

:

  1.         static int[] DoNumbersThing()
  2.         {
  3.             const int MIN = 1;
  4.             const int MAX = 43;
  5.  
  6.             int intRnd = 0;
  7.             Random rnd = new Random();
  8.             List<int> intList = new List<int>(MAX);
  9.             while (intList.Count != intList.Capacity - MIN)
  10.             {
  11.                 intRnd = rnd.Next(MIN, MAX);
  12.                 if (!intList.Contains(intRnd))
  13.                     intList.Add(intRnd);
  14.                 else continue;
  15.             }
  16.             return intList.ToArray();
  17.         }


Freaky Chris Apr 29th, 2008 2:45 PM

Re: populate array with random nonrepeating #'s
 
Quote:

Originally Posted by Alias (Post 144527)
You want to fill an array with a length of fourty-two, with numbers one to fourty-two and further yet the numbers should be at random points in the array, right?

If I got that right, why not just fill the array with values one through to fourty-two and manually shuffle the array?

Indeed this is a good way of looking at doing this, it would sure that you do notn have duplicates, and you could simple use a random number to access different slots of the array. Using the same technique to ensure that you do not choose the same slot.

But working with what you have, you may find that the reason you have a 0 at the end is simple you have declared and array of size 43 instead of 42, not sure on that but quite often that is the reason behind it.

Alias, just added a method Using ArrayLists or Vectors, not sure if C# uses arraylists im sure he will clarify which it is, which does indeed make things simpler for you.

Chris

mbd Apr 29th, 2008 6:24 PM

Re: populate array with random nonrepeating #'s
 
If you add the following classes to your project, you can populate a list like this:
:

  1. List<int> list = new List<int>();
  2. list.AddRange(new RandomizedSeries(0, 20));


Series.cs
:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace RandomUnique
  7. {
  8.     class Series : IEnumerable<int>, IEnumerator<int>
  9.     {
  10.         private readonly int begin, end;
  11.         private bool counting = false;
  12.         private int current;
  13.  
  14.         public Series(int end) : this(0, end)
  15.         {
  16.         }
  17.  
  18.         public Series(int begin, int end)
  19.         {
  20.             this.begin = begin;
  21.             this.end = end;
  22.         }
  23.  
  24.         #region IEnumerable<int> Members
  25.  
  26.         public IEnumerator<int> GetEnumerator()
  27.         {
  28.             return this;
  29.         }
  30.  
  31.         #endregion
  32.  
  33.         #region IEnumerable Members
  34.  
  35.         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  36.         {
  37.             return this;
  38.         }
  39.  
  40.         #endregion
  41.  
  42.         #region IEnumerator<int> Members
  43.  
  44.         public int Current
  45.         {
  46.             get { return current; }
  47.         }
  48.  
  49.         #endregion
  50.  
  51.         #region IDisposable Members
  52.  
  53.         public void Dispose()
  54.         {
  55.         }
  56.  
  57.         #endregion
  58.  
  59.         #region IEnumerator Members
  60.  
  61.         object System.Collections.IEnumerator.Current
  62.         {
  63.             get { return Current; }
  64.         }
  65.  
  66.         public bool MoveNext()
  67.         {
  68.             if (!counting)
  69.             {
  70.                 current = begin;
  71.                 counting = true;
  72.             }
  73.             else
  74.             {
  75.                 counting = ++current < end;
  76.             }
  77.             return counting;
  78.         }
  79.  
  80.         public void Reset()
  81.         {
  82.             counting = false;
  83.         }
  84.  
  85.         #endregion
  86.     }
  87. }


RandomizedSeries.cs
:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace RandomUnique
  7. {
  8.     class RandomizedSeries : IEnumerable<int>, IEnumerator<int>
  9.     {
  10.         private Random random = new Random();
  11.         private readonly int begin, end;
  12.         private bool counting = false;
  13.         private int index = 0;
  14.         private List<int> list;   
  15.  
  16.         public RandomizedSeries(int end) : this(0, end)
  17.         {
  18.         }
  19.  
  20.         public RandomizedSeries(int begin, int end)
  21.         {
  22.             this.begin = begin;
  23.             this.end = end;
  24.         }
  25.  
  26.  
  27.  
  28.         #region IEnumerable<int> Members
  29.  
  30.         public IEnumerator<int> GetEnumerator()
  31.         {
  32.             return this;
  33.         }
  34.  
  35.         #endregion
  36.  
  37.         #region IEnumerable Members
  38.  
  39.         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  40.         {
  41.             return this;
  42.         }
  43.  
  44.         #endregion
  45.  
  46.         #region IEnumerator<int> Members
  47.  
  48.         public int Current
  49.         {
  50.             get
  51.             {
  52.                 return list[index];
  53.             }
  54.         }
  55.  
  56.         #endregion
  57.  
  58.         #region IDisposable Members
  59.  
  60.         public void Dispose()
  61.         {
  62.         }
  63.  
  64.         #endregion
  65.  
  66.         #region IEnumerator Members
  67.  
  68.         object System.Collections.IEnumerator.Current
  69.         {
  70.             get { return Current; }
  71.         }
  72.  
  73.         public bool MoveNext()
  74.         {
  75.             if (!counting)
  76.             {
  77.                 list = new List<int>();
  78.                 list.AddRange(new Series(begin, end));
  79.                 counting = true;
  80.             }
  81.             else
  82.             {
  83.                 list.RemoveAt(index);
  84.             }
  85.             index = random.Next(0, list.Count);
  86.             return list.Count > 0;
  87.         }
  88.  
  89.         public void Reset()
  90.         {
  91.             counting = false;
  92.         }
  93.  
  94.         #endregion
  95.     }
  96. }


ghg5 Apr 29th, 2008 6:27 PM

Re: populate array with random nonrepeating #'s
 
i got it working but your code was much more concise alias. I might replace mine anyway. Thank you guys for your help.

mbd Apr 30th, 2008 1:18 PM

Re: populate array with random nonrepeating #'s
 
i was thinking about this today and realized that it would make sense to randomize any enumerable. thus:

RandomEnumerable.cs
:

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace RandomEnumerable
  6. {
  7.     public class RandomEnumerable<T> : IEnumerable<T>, IEnumerator<T>
  8.     {
  9.         private readonly Random random;
  10.         private readonly IEnumerable<T> enumerable;
  11.         private List<T> list;
  12.         private int current;
  13.  
  14.         public RandomEnumerable(Random random, IEnumerable<T> enumerable)
  15.         {
  16.             this.random = random;
  17.             this.enumerable = enumerable;
  18.         }
  19.  
  20.         #region IEnumerable<T> Members
  21.  
  22.         IEnumerator<T> IEnumerable<T>.GetEnumerator()
  23.         {
  24.             return this;
  25.         }
  26.  
  27.         IEnumerator IEnumerable.GetEnumerator()
  28.         {
  29.             return this;
  30.         }
  31.  
  32.         #endregion
  33.  
  34.         #region IEnumerator<T> Members
  35.  
  36.         T IEnumerator<T>.Current
  37.         {
  38.             get { return list[current]; }
  39.         }
  40.  
  41.         void IDisposable.Dispose()
  42.         {
  43.         }
  44.  
  45.         object IEnumerator.Current
  46.         {
  47.             get { return list[current]; }
  48.         }
  49.  
  50.         bool IEnumerator.MoveNext()
  51.         {
  52.             if (list == null)
  53.             {
  54.                 list = new List<T>();
  55.                 list.AddRange(enumerable);
  56.             }
  57.             else
  58.             {
  59.                 list.RemoveAt(current);
  60.             }
  61.             current = random.Next(0, list.Count);
  62.             return list.Count > 0;
  63.         }
  64.  
  65.         void IEnumerator.Reset()
  66.         {
  67.             list = null;
  68.         }
  69.  
  70.         #endregion
  71.     }
  72. }


p.s. this forum sure sucks at highlighting c#

mbd Apr 30th, 2008 1:58 PM

Re: populate array with random nonrepeating #'s
 
i suck at writing enumerables... i tried to use mine in a different program and found they needed to be modified. here they are:

Series.cs
:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3.  
  4. namespace RandomEnumerable
  5. {
  6.     public class Series : IEnumerable<int>
  7.     {
  8.         private readonly int begin, end;
  9.  
  10.         public Series(int end)
  11.             : this(0, end)
  12.         {
  13.         }
  14.  
  15.         public Series(int begin, int end)
  16.         {
  17.             this.begin = begin;
  18.             this.end = end;
  19.         }
  20.  
  21.         #region IEnumerable<int> Members
  22.  
  23.         public IEnumerator<int> GetEnumerator()
  24.         {
  25.             return new Enumerator(begin, end);
  26.         }
  27.  
  28.         IEnumerator IEnumerable.GetEnumerator()
  29.         {
  30.             return new Enumerator(begin, end);
  31.         }
  32.  
  33.         #endregion
  34.  
  35.         #region Nested type: Enumerator
  36.  
  37.         private class Enumerator : IEnumerator<int>
  38.         {
  39.             private readonly int begin, end;
  40.             private bool counting = false;
  41.             private int current;
  42.  
  43.             public Enumerator(int begin, int end)
  44.             {
  45.                 this.begin = begin;
  46.                 this.end = end;
  47.             }
  48.  
  49.             #region IEnumerator<int> Members
  50.  
  51.             public int Current
  52.             {
  53.                 get { return current; }
  54.             }
  55.  
  56.             public void Dispose()
  57.             {
  58.             }
  59.  
  60.             object IEnumerator.Current
  61.             {
  62.                 get { return Current; }
  63.             }
  64.  
  65.             public bool MoveNext()
  66.             {
  67.                 if (!counting)
  68.                 {
  69.                     current = begin;
  70.                     counting = true;
  71.                 }
  72.                 else
  73.                 {
  74.                     counting = ++current < end;
  75.                 }
  76.                 return counting;
  77.             }
  78.  
  79.             public void Reset()
  80.             {
  81.                 counting = false;
  82.             }
  83.  
  84.             #endregion
  85.         }
  86.  
  87.         #endregion
  88.     }
  89. }


RandomEnumerable.cs
:

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace RandomEnumerable
  6. {
  7.     public class RandomEnumerable<T> : IEnumerable<T>
  8.     {
  9.         private readonly IEnumerable<T> enumerable;
  10.         private readonly Random random;
  11.  
  12.         public RandomEnumerable(Random random, IEnumerable<T> enumerable)
  13.         {
  14.             this.random = random;
  15.             this.enumerable = enumerable;
  16.         }
  17.  
  18.         #region IEnumerable<T> Members
  19.  
  20.         IEnumerator<T> IEnumerable<T>.GetEnumerator()
  21.         {
  22.             return new Enumerator<T>(random, enumerable);
  23.         }
  24.  
  25.         IEnumerator IEnumerable.GetEnumerator()
  26.         {
  27.             return new Enumerator<T>(random, enumerable);
  28.         }
  29.  
  30.         #endregion
  31.  
  32.         #region Nested type: Enumerator
  33.  
  34.         private class Enumerator<T> : IEnumerator<T>
  35.         {
  36.             private readonly IEnumerable<T> enumerable;
  37.             private readonly Random random;
  38.             private int current;
  39.             private List<T> list;
  40.  
  41.             public Enumerator(Random random, IEnumerable<T> enumerable)
  42.             {
  43.                 this.random = random;
  44.                 this.enumerable = enumerable;
  45.             }
  46.  
  47.             #region IEnumerator<T> Members
  48.  
  49.             T IEnumerator<T>.Current
  50.             {
  51.                 get { return list[current]; }
  52.             }
  53.  
  54.             void IDisposable.Dispose()
  55.             {
  56.             }
  57.  
  58.             object IEnumerator.Current
  59.             {
  60.                 get { return list[current]; }
  61.             }
  62.  
  63.             bool IEnumerator.MoveNext()
  64.             {
  65.                 if (list == null)
  66.                 {
  67.                     list = new List<T>();
  68.                     list.AddRange(enumerable);
  69.                 }
  70.                 else
  71.                 {
  72.                     list.RemoveAt(current);
  73.                 }
  74.                 current = random.Next(0, list.Count);
  75.                 return list.Count > 0;
  76.             }
  77.  
  78.             void IEnumerator.Reset()
  79.             {
  80.                 list = null;
  81.             }
  82.  
  83.             #endregion
  84.         }
  85.  
  86.         #endregion
  87.     }
  88. }


Alias Apr 30th, 2008 4:02 PM

Re: populate array with random nonrepeating #'s
 
mbd, thats alot of work to do nothing.


All times are GMT -5. The time now is 9:43 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC