0% found this document useful (0 votes)
8 views12 pages

Wk10c Using Random

The document explains the concept of objects in programming, specifically focusing on the Random object in C#. It details how to create and use the Random object to generate random integers, doubles, characters, and sentences. Additionally, it provides sample code demonstrating various applications of the Random object in creating arrays and sentences.

Uploaded by

jairo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views12 pages

Wk10c Using Random

The document explains the concept of objects in programming, specifically focusing on the Random object in C#. It details how to create and use the Random object to generate random integers, doubles, characters, and sentences. Additionally, it provides sample code demonstrating various applications of the Random object in creating arrays and sentences.

Uploaded by

jairo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Using a Random

object
Programming I
Narendra Pershad
Centennial College
Topics

 What is an object
 Creating a Random object
 Using the Next() methods
 Use the NextDouble() method
 Some applications of the random object
 Creating arrays of random numbers
 Creating arrays of random chars
 Creating random sentences
What is an object?

 An object is a piece of software that can do things (has


methods attached to it)
 It is created by the new operator from a template (class)
 C# comes with tons of templates (Random, DateTime,
String, …)
 Each of the object that is created from these templates
has its own set of methods.
 Some objects even have data values attached to them.
Creating the Random object
//this should be declared in global scope
static Random rand = new Random();
//c.f static int[] age = new int[34];

//Using a seed gives the same set of random values


//This is important if your set of values needs to be reproduce
static Random rand = new Random(54321);
Using the Next() method of the random
object
 The following show the various use of the random object
//returns an int less than the largest int (231 - 1)
rand.Next();
//returns an int less than the value of max_number
rand.Next(max_number);
//returns an int greater or equal to the min_number
and less than max_number
rand.Next(min_number, max_number);
Using the NextDouble() method of the random
object
//returns a double in the range 0.0 to 1.0 not including 1.0
rand.NextDouble();

//returns a double in the range 0.0 to 10.0 not including 10.0


10 * rand.NextDouble();

//returns a double in the range 30.0 to 35.0 not including 35.0


30 + 5 * rand.NextDouble();
A sample using random numbers
//create the random object
static Random rand = new Random();

//declare and allocate for 100 int element


int[] numbers = new int[100];

//loop to set each item to a random value


for(int i = 0;i < 100;i++)
{
numbers[i] = rand.Next(10, 20);
}
A sample using random characters
//create the random object
static Random rand = new Random();

//declare and allocate for 100 int element


char[] letters = new char[100];

//loop to set each item to a random value


for(int i = 0;i < 100;i++)
{
letters[i] = (char)rand.Next('A', 'Z');
}
A sample using random numbers
//create the random object
static Random rand = new Random();
//declare and allocate for 100 int element
double[] numbers = new double[100];

//loop to set each item to a random value


for(int i = 0;i < 100;i++)
{
numbers[i] = rand.Next(10, 20)/10.0;
}
Building random sentences (Part 1
of 2)
//declare the source of my random names
string[] names = {"Bart", "Arya", "Curt", "Eden",
"Fred", "Gina", "Jack", "Dave", "Kate"};

//declare the source of my random pets


string[] pets = {"dog", "cat", "hamster",
"parrot", "buggie", "spider", "rabbit", "snake"};

//declare the resulting sentences


string[] result = new string[10];
Building random sentences (Part 2
of 2)
//loop to build the sentences
for(int i = 0;i < result.Length;i++)
{
string name = names[rand.Next(names.Length)];
string pet = pets[rand.Next(pets.Length)];
int age = rand.Next(5, 10);
result[i] = $"{name} is my pet {pet}. He is {age} months
old.";
}
Summary

 An object is a piece of software that has methods


and values
 A random object may be create with or without a
seed
 The random object may be used to generate
random values (ints, chars, doubles, strings,
sentences …)
 The random object can be used to create
interesting programs.

You might also like