Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 24th, 2009, 12:13 AM
Authorized User
 
Join Date: Jun 2009
Posts: 30
Thanks: 13
Thanked 0 Times in 0 Posts
Post Random value(in C#)

hi.....this is my question...

Write a function that generates a random integer between two integers passed as parameters. Build an ASP.NET page which allows you to enter the lower and upper limits, and generates a set of random numbers in that range.
Guide: You may use the following code for the function that generate the random integer.
Random r = new Random();
int rnd = r.Next(10);
return ((b-a)*(rnd)/10 + a);


my code in ques1.aspx.cs is

publicpartialclassQues1 : System.Web.UI.Page
{
protectedvoid Page_Load(object sender, EventArgs e)
{
double min1, max1,num;
int md,randNum;
Random r=newRandom();
min1 = Convert.ToInt32(TextBox1.Text);
max1 = Convert.ToInt32(TextBox2.Text);
if (IsPostBack)
{
randNum = randomNum(min1, max1);//error appear here
Label1.Text = randomNum + " ";
}
}

int randomNum(int min, int max)
{
Random r = newRandom();
int md = r.Next(10);
return ((max - min) * (md) / 10 + min);
}
}

I couldn't solve it.....can anyone correct the error....thank u

Last edited by Banishah; June 24th, 2009 at 12:16 AM..
 
Old June 24th, 2009, 08:13 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

What error do you receive???

Did you read the documentation about how random (and next) works???
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old June 24th, 2009, 11:43 AM
Authorized User
 
Join Date: Jun 2009
Posts: 30
Thanks: 13
Thanked 0 Times in 0 Posts
Default Random value

The error that i received is input string was not in a correct format
-->min1=Convert.ToInt32(TextBox1.Text) ;
i don't know to correct the error bcos min1 is short integer and only got Convert.ToInt16 and Convert.To32....how to make it equal?

i have read the documentation regarding random....but still don't really understand i just follow the formula given by the question.....can u explain to me what does it really mean by random (and next) ?

Last edited by Banishah; June 24th, 2009 at 11:45 AM..
 
Old June 24th, 2009, 12:07 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Your error is clear enough.. The textbox doesn't contain a number that can be converted to an int.

That because when you first load the page, the textbox is empty...
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
The Following User Says Thank You to gbianchi For This Useful Post:
Banishah (June 27th, 2009)
 
Old June 24th, 2009, 12:31 PM
Authorized User
 
Join Date: Jun 2009
Posts: 30
Thanks: 13
Thanked 0 Times in 0 Posts
Default

Thanks alot...i was able to solve it by placing the min1 into IsPostBack....but i was only able to generate one random number....now i wanted to generate 10 numbers between the min1 and max1 that i enter into the textbox.....

min1=minimum number
max1=maximum number
now i have modify it by adding an array...

if (IsPostBack) {
int min1, max1;
int[] num = newint[10];
min1 =
Convert.ToInt16(TextBox1.Text);
max1 =
Convert.ToInt16(TextBox2.Text);
for (int i = 0; i < 10; i++)
{
num[i] = randomNum(min1, max1);
Label1.Text = num.ToString()+
" ";
}
}
but i am getting error...System.Int32[] in the Label1
 
Old June 24th, 2009, 01:04 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

What error.. do you realize that's easier to just concatenate results directly on the label???
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
Random Picture mani_he Pro PHP 7 February 5th, 2005 12:56 PM
random seed? kiw Pro JSP 1 October 31st, 2004 03:27 PM
random number isheikh PHP How-To 1 October 25th, 2004 08:43 PM
Random Numbers not so random... katsarosj ASP.NET 1.0 and 1.1 Basics 5 November 20th, 2003 12:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.