Vivek Kumar Vishwas

Vivek Kumar Vishwas

  • NA
  • 115
  • 56.3k

How to generate Multiple unique Random Number in Asp.net C#

Nov 8 2017 6:46 AM
Hello,
 Actually i want to generate more than 1 random  from given reng od minumum and maximum value from textbox on button click
 
 
. aspx..-------------
<div>
Minimum value<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
Maximum value<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<br />
Result : <asp:Label ID="lblnumber" runat="server"></asp:Label>
<asp:Button ID="Button1" runat="server"
OnClick="Button1_Click" Text="Generate Random Number in a Rage" /><br />
</div>
 
 
 
.cs-----------
protected void Button1_Click(object sender, EventArgs e)
{
//------1
Random r1 = new Random();
int num = r1.Next(
Convert.ToInt32(TextBox1.Text),
Convert.ToInt32(TextBox2.Text)
);
////----- 2
//Random r2 = new Random();
//int num2 = r2.Next(
// Convert.ToInt32(TextBox1.Text),
// Convert.ToInt32(TextBox2.Text)
// );
////----- 2
//Random r3 = new Random();
//int num3 = r3.Next(
// Convert.ToInt32(TextBox1.Text),
// Convert.ToInt32(TextBox2.Text)
// );
////----- 2
//Random r4 = new Random();
//int num4 = r4.Next(
// Convert.ToInt32(TextBox1.Text),
// Convert.ToInt32(TextBox2.Text)
// );
lblnumber.Text = "First No." + num.ToString(); // +", 2nd No." + num2.ToString() + ", No.3 " + num3.ToString() + ", 4th No." + num4.ToString();
//Response.Write(num.ToString());
NewNumber();
}
public Random a = new Random(); // replace from new Random(DateTime.Now.Ticks.GetHashCode());
// Since similar code is done in default constructor internally
public List<int> randomList = new List<int>();
int MyNumber = 0;
private void NewNumber()
{
MyNumber = a.Next(0, 10);
if (!randomList.Contains(MyNumber))
randomList.Add(MyNumber);
}
 
 
With is code , i get only one random number on every click, But i want minimum 4 random number .
Please chack and resolve this problem. 

Answers (1)