Generating random numbers#
int Random.Shared.Next()
Example output:
for (var i = 0; i < 3; ++i)
Console.WriteLine(Random.Shared.Next());
615160908
159352573
1157669699
Run again:
1150926324
467954522
1664251303
Activity 30 (Generating random numbers in an interval)
Write code that generates five random numbers in the range [0, 4] and prints them.
Do you think that this way of generating numbers is useful in our problem? Why / how would you use it?
Selecting a random item#
Instead of generating an index to choose items, we can choose an item directly.
T[] Random.Shared.GetItems(T[] choices, int length)
T[] means any type.
Example:
string[] machines = ["CNC-Lathe-01", "CNC-Mill-02", "CNC-Grind-03", "5-Axis-04", "EDM-05"];
const int inspectedMachineCountPerWeek = 2;
var pickedMachinesForInspection = Random.Shared.GetItems(machines, inspectedMachineCountPerWeek);
foreach (var machine in pickedMachinesForInspection)
Console.WriteLine(machine);
Example Output:
CNC-Lathe-01
CNC-Grind-03
Activity 31
Implement a weekly plan for a spare parts quality check. Example output:
Weekly Schedule:
Mon: hydraulic pump
Tue: PLC module
Wed: servo motor
Fri: control valve
Use the following template.
string[] qcDays = ["Mon", "Tue", "Wed", "Fri"];
string[] parts = ["servo motor", "pressure sensor", "control valve", "flow meter"];