![]() |
| | #1 |
| Registered User Join Date: Jan 2006 Location: Texas
Posts: 10
| Lesson #3 - Math - take a numerical user input and convert it to an integer - generate a random number - take a look at a simple if statement - and add 2 numbers. Before we begin, I would like to note that the only instance of math used is addition. However you might want to explore further by substituting the operators. (subtraction (-), multiplication (*) and division(/)) I would also like to note that when doing division, if variabes of int type are used you will not get any remainders (i.e. n.345984) To be able to work with real numbers, numbers with decimals, you can use float. Below is a way to declare a float and assign it a value: float floatNumber; floatNumber = 22.34f; Code:
using System;
namespace lessonThree
{
///<summary>
/// In this lesson we will cover some basic math
/// operations, look at an if statement and a few other
/// useful things that should be understood when
/// programming in C#
///</summary>
class thirdLesson
{
///<summary>
/// Ask an age.
/// If under 18 -> Exit Program
/// Give lucky number for the day
/// Predict when person will get rich.
///</summary>
[STAThread]
static void Main(string[] args)
{
int age;
//Random object createRand
Random createRand = new Random();
int luckyNumber;
Console.Write("Enter your age: ");
//grab the input from user and convert it to
//an integer, hence the 'Convert.ToInt32();
age = Convert.ToInt32(Console.ReadLine());
//skip line
Console.WriteLine();
if (age > 17)
{
//Random generated numbers range from 0 to 1,
//therefore we multiply by ten (10) below
//The .NextDouble() method helps retrieve the value
luckyNumber = (int)(createRand.NextDouble()*10);
Console.WriteLine("Your Lucky Number is: {0}", luckyNumber);
//Here you use the value of luckyNumber and add it to his age
Console.WriteLine("You are {0} years old and will be rich at age: {1}", age, age+luckyNumber);
//As you can see the zero in braces {0} holds the variable age
//and the one in braces {1} holds the sum of the variables "age and luckyNumber" age+luckyNumber
}//If the age is not 18 or above we conclude by saying
Console.WriteLine("Thanks and Have a Great Day");
Console.WriteLine();
Console.WriteLine("Press [ENTER] to Exit");
Console.ReadLine();
}//end Main()
}//end class
}//end namespace |
| oval is offline | |
| | #2 | |
| and the Hat of Clumsiness Join Date: Oct 2002
Posts: 1,101
| Code: using System;
using System.Collections.Generic;
using System.Text;
namespace TestRand {
public class Program {
private Random randomField = new Random();
static void Main(string[] args) {
Program o = new Program();
//Start generating 10 random numbers
Console.WriteLine("Random from within main");
Random randinMain = new Random();
for (int i = 0; i < 10; i++) {
Console.WriteLine(randinMain.Next(100));
}
Console.WriteLine("Random from within function");
o.RandinFunction();
Console.WriteLine("Random with function random object passed as argument to function");
o.RandWithField(o.randomField);
Console.WriteLine("Random using a field no function");
for (int i = 0; i < 10; i++) {
Console.WriteLine(o.randomField.Next(100));
}
Console.WriteLine("Random using a field - function");
o.RandWithField(o.randomField);
Console.ReadLine();
}
/// <summary>
/// generate 10 random numbers with random object in function
/// </summary>
public void RandinFunction() {
Random randInFunction = new Random();
for (int i = 0; i < 10; i++) {
Console.WriteLine(randInFunction.Next(100));
}
}
/// <summary>
/// generate 10 random numbers with random object from field
/// </summary>
/// <param name="r"></param>
public void RandWithField(Random r) {
for (int i = 0; i < 10; i++) {
Console.WriteLine(r.Next(100));
}
}
}
}
Quote:
Just wanted to add this since I've come across this thing several times and people keep wondering why they always have the same "random" numbers, it probably has something to do with how the rand is seeded but anyway ... solution is to just use a field instead of a local object. | |
| GanglyLamb is offline | |
| | #3 |
| Registered User Join Date: Jan 2006 Location: Texas
Posts: 10
| Thanks for the additional info GL. |
| oval is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Math | knightjp | General Discussions | 16 | 04-01-2009 05:36 PM |
| Help with C++ Math | aonic | C++ Programming | 4 | 01-29-2005 04:40 AM |
| Basic Math Problem. Undefined Math Functions | gsoft | C Programming | 1 | 12-28-2004 03:14 AM |
| Math Header? | Rune Hunter | C++ Programming | 26 | 09-17-2004 06:39 AM |
| toughest math course | axon | A Brief History of Cprogramming.com | 12 | 10-28-2003 10:06 PM |