[C#] C# Random Object

Date:     Updated:

Categories:

Tags:

📋 This is my note-taking from what I learned in the class “Programming 1 - COMP 100-002”


C# Random Object

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 reproduced
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 (2^31 - 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();


Example 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);
}


Example 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');
}


Example building random sentences

//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];

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




Back to Top

See other articles in Category CS

Leave a comment