Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C# Programming

C# Programming C# (pronounced C-sharp) is a new object oriented language from Microsoft and is derived from C and C++. It also borrows a lot of concepts from Java too including garbage collection.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-29-2008, 02:12 AM
gaylo565's Avatar   
gaylo565 gaylo565 is online now
Learning Programmer
 
Join Date: May 2007
Location: flagstaff, az
Posts: 97
Last Blog:
String Manipulation wi...
Rep Power: 6
gaylo565 will become famous soon enoughgaylo565 will become famous soon enough
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 02:18 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 04-29-2008, 02:58 PM
excavator excavator is offline
Newbie
 
Join Date: Apr 2008
Location: behind my desk
Posts: 16
Rep Power: 1
excavator is on a distinguished road
Default Re: filling an array with random #s

Code:
private int[] PopulateRandomNumbers(int arraylength, int minvalue, int maxvalue)
        {
            DateTime c = DateTime.Now;
            Random r = new Random(c.Millisecond);
            int[] RandomNumbers = new int[arraylength];
            int RandomNumber;

            for (int i = 0; i < arraylength; i++)
            {
                RandomNumber = r.Next(minvalue, maxvalue);
                while (isExistIn(RandomNumber, RandomNumbers) == true)
                {
                    RandomNumber = r.Next(minvalue, maxvalue);
                }
                RandomNumbers[i] = RandomNumber;                
            }
            return RandomNumbers;
        }

        private Boolean isExistIn(int number, int[] Numbers)
        {
            Boolean result = false;
            for (int i = 0; i < Numbers.Length; i++)
            {
                if (Numbers[i] == number)
                {
                    result = true;                    
                }
            }
            return result;
        }
to use the code
Code:
int[] RN = PopulateRandomNumbers(40, 1, 42);
there may be simpler steps but i hope this can help u a bit.
__________________
Information Technology Talk
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-29-2008, 03:10 PM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 3,471
Last Blog:
Web slideshow in JavaS...
Rep Power: 30
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default Re: filling an array with random #s

It's not completely necessary to instantiate so many objects, but this makes it clearer to read/understand. Good help, though. +rep given.

Welcome to CodeCall anyhow, excavator!
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-29-2008, 06:36 PM
gaylo565's Avatar   
gaylo565 gaylo565 is online now
Learning Programmer
 
Join Date: May 2007
Location: flagstaff, az
Posts: 97
Last Blog:
String Manipulation wi...
Rep Power: 6
gaylo565 will become famous soon enoughgaylo565 will become famous soon enough
Default Re: filling an array with random #s

thank u...i finally got mine workin but yours is much more concise. I might still switch what ive got out for what u have
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-30-2008, 11:01 AM
excavator excavator is offline
Newbie
 
Join Date: Apr 2008
Location: behind my desk
Posts: 16
Rep Power: 1
excavator is on a distinguished road
Default Re: filling an array with random #s

@Xav
I agree, and thanks.

@gaylo565
glad i can be of support
__________________
Information Technology Talk
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 05-02-2008, 11:21 AM
Xav's Avatar   
Xav Xav is offline
Code Warrior
 
Join Date: Mar 2008
Location: London, England
Posts: 3,471
Last Blog:
Web slideshow in JavaS...
Rep Power: 30
Xav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to allXav is a name known to all
Send a message via MSN to Xav
Default Re: filling an array with random #s

You're welcome. Not many people use the reputation system, so we try to encourage it as much as possible!
__________________
Xav, the power of youth
Worship the Creator... not his creations
Web Site | Beta Site
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Encryption using Random!? TcM Programming Theory 14 01-07-2008 10:48 AM
Random Elements From Array Victor Java Help 8 12-01-2007 09:38 PM
HELP! 3x3 array random number 1-9 no repeat siren C and C++ 3 09-25-2007 02:03 PM
Return random numbers without duplicates Paradine PHP Tutorials 0 08-26-2007 02:07 PM
Python 2D array question annannienann Python 3 04-23-2007 04:36 PM


All times are GMT -5. The time now is 10:32 AM.

Contest Stats

Xav ........ 164.00000
dargueta ........ 128.00000
John ........ 127.00000
gaylo565 ........ 18.00000
XaNaX ........ 15.00000
Johnnyboy ........ 3.00000
navghost ........ 1.00000

Contest Rules

Ads