C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-08-2005, 11:51 AM   #1
Registered User
 
Join Date: Feb 2005
Posts: 1
Exclamation non repeating random number generation?

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 02-08-2005, 05:23 PM   #2
Registered User
 
Join Date: Jun 2003
Posts: 90
I use something like this for my code i am using in my project -

Code:
Stack numberCheck = new Stack();
int number = 0;
Random randomNumber = new Random();
bool exists = false;

//generates a number then checks if it has already been made

number = randomNumber.Next(1,100);
exists = numberCheck.Contains(number);
while(exists == true)
{
//shrink this code down later by making a function of your own
//which can also be used above too
number = randomNumber.Next(1,100);
exists = numberCheck.Contains(number);
}
Should be fairly easy to see where you can change things to suit your needs.

And use the Convert to change the int to a string ie. -

string word = Convert.tostring(number);
__________________
He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

The fool wonders, the wise man asks. - Benjamin Disraeli

There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz
DanFraser is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
xor linked list adramalech C Programming 23 10-14-2008 10:13 AM
Logical errors with seach function Taka C Programming 4 09-18-2006 05:20 AM
Counting number from a random file kamisama C Programming 42 02-22-2005 05:16 PM
random number between negative and positive number anomaly C++ Programming 6 12-06-2003 08:40 AM
Random Number Generation drdroid A Brief History of Cprogramming.com 21 08-02-2003 03:35 AM


All times are GMT -6. The time now is 07:35 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22