Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 8th, 2005, 12:02 PM   #1
gencor45
Newbie
 
Join Date: Feb 2005
Posts: 20
Rep Power: 0 gencor45 is on a distinguished road
Exclamation non repeating random number generation

Today, 12:51 PM Any help would be appreciated!
Im new to C# and im going crazy trying to figure out a simple way to create a 4-digit non-repeating random number for a number guessing game. If anyone can help with the code I already have I would be grateful.

Heres what I have:


Code:
private void form1_Load(object sender, System.EventArgs e)
        {
string answer;
            
//Creates random number array

String[] myNumbers={"0","1","2","3","4","5","6","7","8","9"};
Random rndNumbers=new Random();

I dont know what kind of loop make from here, and then I dont understand how to put the random number into a string

Please help
gencor45 is offline   Reply With Quote
Old Feb 8th, 2005, 12:41 PM   #2
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
check if the random number generated has already been generated, if so generate a new one, if not, use it.

good luck

Dizz
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Feb 9th, 2005, 12:11 AM   #3
bobc
Newbie
 
Join Date: Feb 2005
Posts: 16
Rep Power: 0 bobc is on a distinguished road
Here is a simple one using a form, a label, and a button to generate the random number (4 digits padded with zero's).

private void button1_Click(object sender, System.EventArgs e)
		{
			Random rnd = new Random();
			int rndNumber = rnd.Next(9999);
			label1.Text = rndNumber.ToString("0000");
		}

If you want to make sure there are no duplicate numbers, you must put each generated number in an array or array list. Every time you click the button you must loop through the generated numbers and compare them to the newly generated number. If there is a duplicate number you must generate another until it is unique.

Hope this helps.
bobc
bobc is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:47 PM.

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