![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2005
Posts: 20
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Professional Programmer
|
check if the random number generated has already been generated, if so generate a new one, if not, use it.
good luck Dizz |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Feb 2005
Posts: 16
Rep Power: 0
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|