View Single Post
  #1 (permalink)  
Old 04-29-2008, 03:12 AM
gaylo565's Avatar   
gaylo565 gaylo565 is offline
Programmer
 
Join Date: May 2007
Location: flagstaff, az
Posts: 187
Last Blog:
String Manipulation wi...
Rep Power: 9
gaylo565 is a jewel in the roughgaylo565 is a jewel in the roughgaylo565 is a jewel in the rough
Default filling an array with random #s

I am trying to fill an array with random #s from 1-42 with out any repetition...is there a shuffle sort of function that I am unaware of? Not sure whats wrong with my code but I am getting some #'s more than once when I print the array to a text file. Some help would be nice
Code:
private void frmMain_Load(object sender, System.EventArgs e)
		{
			//seed the random number function
			DateTime dtmCurrent = DateTime.Now;
			generateRandom = new Random(dtmCurrent.Millisecond);

			//fill array with random #'s from 1 to 7
			int intAdd = 0;
			while(intAdd < 42)
			{
				intStore = generateRandom.Next(1,42);
				//check to see if array contains generated #
				foreach(int intBoard1 in intBoard)
				{
					if(intStore == intBoard1)
					{
						bln_isSame = true;
					}
					else
					{
						bln_isSame = false;
					}
				}
				//if array doesn't contain generated number then add                                           it to the array in next position
				if(bln_isSame == false)
				{
					intBoard[intAdd] = intStore;
					intAdd++;
				}
			}
			int intIndex=0;
			//write array to a file for test
			StreamWriter arrayStreamWriter = new StreamWriter("C:\\Documents and Settings\\Owner.Gir\\My Documents\\Visual Studio Projects\\Memory\\array.txt",true);
			while(intIndex < 42)
			{
				arrayStreamWriter.WriteLine(intBoard[intIndex]);
				intIndex++;
			}
			arrayStreamWriter.Close();
		}
there must be a simple way of doing this.
there are a few variables declared at the class level wich isnt shown but thats not where my problem is...It compiles ok but not what I need.

Last edited by gaylo565; 04-29-2008 at 03:18 AM.
Reply With Quote

Sponsored Links