Click here to Skip to main content
13,664,561 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hello,
Actually i want to generate more than 1 random from given reng od minumum and maximum value from textbox on button click


. aspx..-------------

Minimum value<asp:TextBox ID="TextBox1" runat="server">

Maximum value<asp:TextBox ID="TextBox2" runat="server">



Result : <asp:Label ID="lblnumber" runat="server">
<asp:Button ID="Button1" runat="server"
OnClick="Button1_Click" Text="Generate Random Number in a Rage" />





.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.

What I have tried:

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. 
Posted 8-Nov-17 0:52am
Updated 9-Nov-17 1:30am
Comments
F-ES Sitecore 8-Nov-17 7:00am
   
Use a "for" loop to execute the code that gets the random number the required number of times.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

First off, don't use Convert for user input: always use TryParse:
int min, max;
if (!int.TryParse(textBox1.Text, out min))
   {
   // Report bad input to user
   return;
   }
if (!int.TryParse(textBox2.Text, out max))
   {
   // Report bad input to user
   return;
   }
for (int i = 0; i < 4; i++)
   {
   randomList.Add(rand.Next(min, max));
   }
  Permalink  
Comments
CPallini 8-Nov-17 7:16am
   
5.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

protected void Button1_Click(object sender, EventArgs e)
    {
        int min, max,v1,v2,v3,v4;
        min = int.Parse(TextBox1.Text);
        max = int.Parse(TextBox2.Text);
        Random r = new Random();
        v1 = r.Next(min,max);
        v2 = r.Next(min,max);
        v3 = r.Next(min, max);
        v4 = r.Next(min, max);
        Label1.Text = v1.ToString()+" "+ v2.ToString() +" "+v3.ToString()+" "+ v4.ToString();

    }

OUTPUT:-
min value 100
max value 220
result 180 137 186 202
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180810.1 | Last Updated 9 Nov 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100