• No results found

1.03-Randomize Output.pptx

N/A
N/A
Protected

Academic year: 2020

Share "1.03-Randomize Output.pptx"

Copied!
20
0
0

Loading.... (view fulltext now)

Full text

(1)

Randomize Output

(2)

Random Class

Soon you will learn about classes. Classes can be built in or created by the programmer.

For pseudo random output- we use the Random class.

(3)

Coding Example

Random numGen = new Random(); //creating a new Random object

int num1= numGen.Next(0,15); //using the Next method to generate a number

MessageBox.Show(num1.ToString()); //output- remember we must convert to a string

• Note: The 2nd argument is not included in the range- so the highest random

(4)

Static Keyword

• To create better (more random) random numbers we can make the data member static.

private void button1_Click(object sender, EventArgs e) {

randNum();

}

(5)

RandNum Method

static Random _r = new Random(); //creating an instance variable

static void randNum() {

int num1 = _r.Next(0,15);

(6)

Static in C#

As mentioned in 1.01- the “static” keyword works differently in C#.

Classes, methods, and constructors (you will learn more about each of these topics in later lessons) can be static.

(7)

Program.cs

Every C# program has a static method. It is called the main method.

static void Main(string[] args)

(8)

Random Characters

• Random isn’t only for numbers! We can generate random characters too.

private void btnStart_Click(object sender, EventArgs e) {

char randLetter = RandomLetter.GetLetter();

MessageBox.Show(randLetter.ToString()); //must convert char to string

(9)

Class code

public static class RandomLetter //static custom class {

static Random _randLetter = new Random(); //static variable

public static char GetLetter() //method that returns a value of type char {

int num1 = _randLetter.Next(0, 26); char letter = (char)('a' + num1); return letter;

(10)

Program: Random Letters

(11)

Step 1: Radio Buttons

Add 2 radio buttons on the form for the selection: upper or lower case letter.

(12)

Step 2: Passing the Choice to the

Method

• Since a custom class cannot access objects on the form we will pass a value to let it know the user’s choice.

int letterChoice = 0;

if(radUpper.Checked) {

letterChoice = 1; }

(13)

Step 3: Method

if(letterChoice == 0) {

char letter = (char)('a' + num1); return letter;

} else {

char letter = (char)('A' + num1); return letter;

(14)

Video

(15)

Random Characters

What if we need more than 1 random character?

There is an easy way to get up to 11 random characters.

(16)

Using Keyword

At the top of your program you see this: (some lines omitted)

using System.Data;

using System.Windows.Forms;

These imports give C# access to classes. We need to add the IO class which the path method is part of.

(17)

Program Code

private void btnStart_Click(object sender, EventArgs e) {

string randomString = GetRandomString(); //variable to hold output from method lblOutput.Text= randomString;

}

public static string GetRandomString() //string method which will return a random string

{

string path = Path.GetRandomFileName();

path = path.Replace(".", ""); // Remove period. //GetRandomFileName method adds a period

(18)

Combining Random Numbers with a

Timer

A timer can be combined with a random number generator to do cool things.

(19)

Challenge Program

Write a program that can generate a random number between 1 and 5.

(20)

Conclusion

References

Related documents

Knowledge of the impedance of the various elements in a circuit allows us to apply any of the circuits analysis methods (KVL, KCL, nodal, superposition Thevenin etc.) for the

The third section of the book pays specific attention to simulation and gaming theory in intercultural business settings and is, in my view, the most interesting part of the

NOTE 2: No degradation of performance after the test is understood as no degradation below a minimum performance level specified by the manufacturer for the use of the apparatus as

Online measurements of particle- and gas-phase chemistry in the new Cambridge Atmospheric Simulation Chamber (CASC) have been achieved simultaneously using comple- mentary

Having brought Klein into his own “field” by converting the puerile into the archaic, Lacan then goes on to summarize, immediately after the little fable of the match boxes, an

The results of our study show that with a simple yet thorough training we could achieve good inter-observer reliability between trained readers and an experienced reader for

It is useful to emphasize that the big bang theory allows for many possible models (depending on cosmological parameters such as the space curvature, the

The process includes using SAS programming to generate random numbers and using the SAS procedure PROC PLAN to generate the randomization schedule.. RANDOM