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

You are currently viewing the ASP.NET 3.5 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, 06:55 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...
1. 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);


and my code 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);
}
}
and anyone solve my error......

Last edited by Banishah; June 24th, 2009 at 06:59 AM..
 
Old June 24th, 2009, 12:25 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

You have several problems.

The main problem is that this code doesn't even compile correctly, as you are calling randomNum incorrectly when setting the label text.

The second problem is that this logic does not belong in the Page_Load method, It should be placed in an event handler for the control doing the postback (presumably a button click).

Third, your logic is way too complex. You are declaring variables that you don't need, and declaring them as the wrong type. Also, there is no need to call the Randon constructor twice.

Code:
using System;
public partial class Ques1 : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
   }
   protected void Button1_Click(object sender, EventArgs e)
   {
      int min = Convert.ToInt32(TextBox1.Text);
      int max = Convert.ToInt32(TextBox2.Text);
      Label1.Text = randomNum(min, max).ToString();
   }
   private int randomNum(int min, int max)
   {
      Random r = new Random();
      int rnd = r.Next(10);
      return ((max - min) * rnd / 10 + min);
   }
}
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
The Following User Says Thank You to Lee Dumond For This Useful Post:
Banishah (June 27th, 2009)
 
Old June 24th, 2009, 01:01 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

Lee.. this thread is duplicate...
Random value(in C#)

and it looks too much like homework..
__________________
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, 01:13 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Yes, I figured it was homework. What's wrong with asking for help with your homework?
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
 
Old June 24th, 2009, 01:17 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

Nothing.. but Isn't a thread for homework?? He's learning, asking is a good way to learn :)...

But isn't better to point him in the right direction, instead of solving the problem???
__________________
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, 01:26 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Quite frankly, based on what he posted, it appeared he didn't have a good grasp on the tools required to even begin solving the problem, no matter how many "pointers" he was given.

I teach some computer classes at a local college. Sometimes, if I assign a series of questions, I find it helps to show the students a complete solution to one of the easy ones. Then, by studying my solution, they can work out the rest on their own.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
The Following User Says Thank You to Lee Dumond For This Useful Post:
Banishah (June 27th, 2009)





Similar Threads
Thread Thread Starter Forum Replies Last Post
random sort Bernardo Pacheco XSLT 9 September 30th, 2012 06:28 AM
Random value(in C#) Banishah ASP.NET 2.0 Basics 5 June 24th, 2009 01:04 PM
Random time rajanikrishna Classic ASP Databases 0 March 1st, 2004 10:10 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.