Need the code for Randomization of numbers using c#
-
hi try with the below code protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ""; int Quescnt = 6; string Randomnos = string.Empty; for (int i = 0; i < Quescnt; i++) { int RandomNumber = RandomClass.Next(28, 37); if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ",")) { Randomnos = Randomnos + RandomNumber.ToString() + ", "; } else { Quescnt++; } } Label1.Text = Randomnos; } and try with this too[this will go to infinite loop] protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ""; int Quescnt = 6; string Randomnos = string.Empty; for (int i = 0; i < Quescnt; i++) { int RandomNumber = RandomClass.Next(28, 33); if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ",")) { Randomnos = Randomnos + RandomNumber.ToString() + ", "; } else { Quescnt++; } } Label1.Text = Randomnos; }
I am not even going to look at them until you have preserved the formatting - why should I even slightly struggle? Remember the "code block" button above?
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
hi try with the below code protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ""; int Quescnt = 6; string Randomnos = string.Empty; for (int i = 0; i < Quescnt; i++) { int RandomNumber = RandomClass.Next(28, 37); if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ",")) { Randomnos = Randomnos + RandomNumber.ToString() + ", "; } else { Quescnt++; } } Label1.Text = Randomnos; } and try with this too[this will go to infinite loop] protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ""; int Quescnt = 6; string Randomnos = string.Empty; for (int i = 0; i < Quescnt; i++) { int RandomNumber = RandomClass.Next(28, 33); if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ",")) { Randomnos = Randomnos + RandomNumber.ToString() + ", "; } else { Quescnt++; } } Label1.Text = Randomnos; }
I will refrain form making any comment except to point out that you have the following for loop:
for (int i = 0; i < Quescnt; i++)
and within this loop you have the following statement
Quescnt++;
Using the debugger could have helped you locate this fairly quickly.
-
hi try with the below code protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ""; int Quescnt = 6; string Randomnos = string.Empty; for (int i = 0; i < Quescnt; i++) { int RandomNumber = RandomClass.Next(28, 37); if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ",")) { Randomnos = Randomnos + RandomNumber.ToString() + ", "; } else { Quescnt++; } } Label1.Text = Randomnos; } and try with this too[this will go to infinite loop] protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ""; int Quescnt = 6; string Randomnos = string.Empty; for (int i = 0; i < Quescnt; i++) { int RandomNumber = RandomClass.Next(28, 33); if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ",")) { Randomnos = Randomnos + RandomNumber.ToString() + ", "; } else { Quescnt++; } } Label1.Text = Randomnos; }
vasavi.p wrote:
and try with this too[this will go to infinite loop]
Now for extra credit (that is, beyond being able to blindly paste your code into this window) can you tell us why this goes into an infinite loop? Hint:- Documentation :Random.Next(Int32,Int32)[^] Relevant part of documentation:
minValue Type: System..::.Int32 The inclusive lower bound of the random number returned. maxValue Type: System..::.Int32 The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.
-
I will refrain form making any comment except to point out that you have the following for loop:
for (int i = 0; i < Quescnt; i++)
and within this loop you have the following statement
Quescnt++;
Using the debugger could have helped you locate this fairly quickly.
-
hi try with the below code protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ""; int Quescnt = 6; string Randomnos = string.Empty; for (int i = 0; i < Quescnt; i++) { int RandomNumber = RandomClass.Next(28, 37); if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ",")) { Randomnos = Randomnos + RandomNumber.ToString() + ", "; } else { Quescnt++; } } Label1.Text = Randomnos; } and try with this too[this will go to infinite loop] protected void Button1_Click(object sender, EventArgs e) { Label1.Text = ""; int Quescnt = 6; string Randomnos = string.Empty; for (int i = 0; i < Quescnt; i++) { int RandomNumber = RandomClass.Next(28, 33); if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ",")) { Randomnos = Randomnos + RandomNumber.ToString() + ", "; } else { Quescnt++; } } Label1.Text = Randomnos; }
Read this first How to post questions[^]
-
Irrespective of this error, s/he's not going to get 6 unique random numbers from a range of only 5 possibilities.
J4amieC wrote:
Irrespective of this error, s/he's not going to get 6 unique random numbers from a range of only 5 possibilities.
I thought s/he's complaining that it goes into an infinite loop. There seems little point in worrying about anything else until that logic is fixed.
-
J4amieC wrote:
Irrespective of this error, s/he's not going to get 6 unique random numbers from a range of only 5 possibilities.
I thought s/he's complaining that it goes into an infinite loop. There seems little point in worrying about anything else until that logic is fixed.
-
Well spotted. I found the whole thing far too hard to read, and I always like in the first instance to try to get these people to at least try to use the debugger before asking us to do their job for them. I am losing all faith in development as a respectable profession. I think I should become a plumber.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
Christian Graus wrote:
I think I should become a plumber.
I've been thinking electrician. Plumbing was an idea, but you end up having to deal with septic tanks and pipes clogged with things no one ever wants to know about.
The true man wants two things: danger and play. For that reason he wants woman, as the most dangerous plaything.
-
Well spotted. I found the whole thing far too hard to read, and I always like in the first instance to try to get these people to at least try to use the debugger before asking us to do their job for them. I am losing all faith in development as a respectable profession. I think I should become a plumber.
Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.
I wonder if you can help me? I've been trying to fit a new washer to my kitchen tap for some time. Should I use C# or VB.Net for this?
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
I wonder if you can help me? I've been trying to fit a new washer to my kitchen tap for some time. Should I use C# or VB.Net for this?
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
Neither - it's a hardware problem...
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
I thought it was going into an infinite loop because its looking for the next unique random number, and all possibilities have already been found. :confused:
J4amieC wrote:
I thought it was going into an infinite loop because its looking for the next unique random number, and all possibilities have already been found.
In the following code:
for (int i = 0; i < Quescnt; i++)
{
int RandomNumber = RandomClass.Next(28, 33);
if (!Randomnos.ToString().Contains(RandomNumber.ToString() + ","))
{
Randomnos = Randomnos + RandomNumber.ToString() + ", ";
}
else
{
Quescnt++;
}
}every time it hits the
else
clause it is incrementingQuescnt
so thefor
loop will keep going becauseQuescnt
will always stay ahead of the loop variable.