# Generating random numbers

```cs
int Random.Shared.Next()
```
Example output:

```cs
for (var i = 0; i < 3; ++i)
    Console.WriteLine(Random.Shared.Next());
```
```text
615160908
159352573
1157669699
```
Run again:
```text
1150926324
467954522
1664251303
```

:::{activity} Generating random numbers in an interval
:label: generating-random-numbers-in-an-interval
1. Write code that generates five random numbers in the range [0, 4] and prints them.
2. 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.

```cs
T[] Random.Shared.GetItems(T[] choices, int length)
```
`T[]` means any type.

Example:

```cs
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:
```text
CNC-Lathe-01
CNC-Grind-03
```

:::{activity}
<!-- loops, random numbers, array indexing-->
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.
```cs
string[] qcDays = ["Mon", "Tue", "Wed", "Fri"];
string[] parts = ["servo motor", "pressure sensor", "control valve", "flow meter"];
```
:::

## Appendix

- [System.Random class](https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-random)