C# Random
Roll one die inside the Pine file and prove the number landed between 1 and 6. Keep the two lines you already trust, left=10 right=2 sum=12 and 2026-03-05. Under them, create Random rng = new Random();, draw int roll = rng.Next(1, 7);, and print the roll and a range check. Run with dotnet run. The third line is roll= followed by one number from 1 to 6. The fourth line is inRange=True on every run.
Quality and Wrap-up continues, so Pine, Add, left, right, and the fixed day all stay. The dates page chose 5 March 2026 by hand, and the screen showed 2026-03-05 every time. This page adds the opposite kind of value: a number the program picks for you. That is why the roll line can differ between two runs while the sum line and the date line do not move. If the second line is today's date instead of 2026-03-05, the file still prints DateTime.Now. Put new DateTime(2026, 3, 5) back first, so only one line on the screen is allowed to change.
Random lives in System, like Console and DateTime. The file already has using System;, so no new using line is needed.
Roll one die
Open the Pine file. The class name stays Pine. Keep Add with its four /// lines and the parameters left and right. Keep the tight sum format with no width and the date line with {0:yyyy-MM-dd}. Delete any ReadLine line. The program must not wait for typing.
Under the date, make one generator and draw from it:
Random rng = new Random();
int roll = rng.Next(1, 7);
rng is a local variable in camelCase, the same naming habit as left and right. Random is the type, so it is PascalCase. new Random() builds a generator. Next(1, 7) asks it for one whole number. The first argument is the smallest value you can get. The second argument is one past the largest value you can get. So the roll is 1, 2, 3, 4, 5, or 6. A die has six faces, and seven never appears.
Then print two lines: Console.WriteLine("roll={0}", roll); and Console.WriteLine("inRange={0}", roll >= 1 && roll <= 6);. The second call prints a bool, so the screen shows True with a capital T.
using System;
public class Pine
{
/// <summary>Adds the two stored numbers.</summary>
/// <param name="left">The first number.</param>
/// <param name="right">The second number.</param>
/// <returns>The sum of left and right.</returns>
public static int Add(int left, int right)
{
return left + right;
}
public static void Main()
{
int left = 10;
int right = 2;
int sum = Add(left, right);
Console.WriteLine("left={0} right={1} sum={2}", left, right, sum);
DateTime day = new DateTime(2026, 3, 5);
Console.WriteLine("{0:yyyy-MM-dd}", day);
Random rng = new Random();
int roll = rng.Next(1, 7);
Console.WriteLine("roll={0}", roll);
Console.WriteLine("inRange={0}", roll >= 1 && roll <= 6);
}
}
Four lines, one that moves
Run dotnet run. The screen shows left=10 right=2 sum=12, then 2026-03-05, then roll= and a number such as 4, then inRange=True. Run it again. The first two lines are the same. The roll may be a different number, or the same one by chance. The fourth line is still inRange=True.
That fourth line is the part you can check. You cannot know the roll before the program runs, so this page never asks you to match one exact roll. You can know the rule the roll must obey. roll >= 1 && roll <= 6 is true for every face of a die. If that line ever prints False, the range in Next is wrong, not the die.
Walk Main once. It stores ten and two. Add returns twelve. The first WriteLine fills the three number slots. The date line prints the fixed day. new Random() builds the generator. Next(1, 7) draws once and stores the result in roll. The two last calls print what was drawn and whether it obeys the rule. Nothing about the roll changes sum or day.
The top number is left out
The second argument of Next is exclusive. rng.Next(1, 7) can return 1 and can return 6, and it cannot return 7. The first argument is inclusive. This is why a six-sided die uses 7 and not 6. If you write rng.Next(1, 6), the die has five faces: 1 through 5. Run that version many times and a 6 never appears. The check roll <= 6 still prints True, which is why a range check alone cannot prove every face shows up. Read the bounds you typed.
rng.Next(6) with one argument gives 0 through 5. That is six values, but they start at zero. Adding one, rng.Next(6) + 1, gives 1 through 6, the same faces as Next(1, 7). Pick one form and keep it. This page uses Next(1, 7) because the two numbers name the range you meant.
If the first argument is bigger than the second, such as rng.Next(7, 1), the call stops the run with an error. The generator cannot draw from a range that ends before it starts. Swap the two numbers. Do not catch the failure on this page.
One generator, many draws
Call Next again on the same rng when you want a second roll: int second = rng.Next(1, 7);. Each call draws a new number. You do not need a second new Random() for a second roll. Keep one generator for the run and ask it as often as you need.
A seed fixes the whole sequence. new Random(42) starts from the seed 42, and on the same runtime the same calls return the same numbers on every run. That helps when you want to repeat a test. This page does not use a seed, and it does not promise which number any seed gives. The checked line is still inRange=True, not a particular roll.
Random is for games, samples, and practice. It is not for passwords or security tokens. For those, C# has RandomNumberGenerator in System.Security.Cryptography. That is outside this page. If a sample from another language uses Math.random(), that is not the C# call. In C#, the draw is a method on a Random value, such as rng.Next(1, 7).
📊 Three ways to call Random in the Pine file
| Name | What it gives |
|---|---|
| Range | rng.Next(1, 7) gives 1 through 6, and 7 is never drawn |
| Zero | rng.Next(6) gives 0 through 5, so add 1 for a die |
| Seed | new Random(42) repeats the same sequence on each run of the same runtime |
A wrong roll is not a wrong sum
If the roll line looks strange, fix the roll line. If the third line says roll=0, the call is Next(6) without the + 1. If you see roll=7, the bounds are not 1, 7; check for Next(1, 8). If the program stops at the draw, the two bounds are in the wrong order. None of those problems touch Add. The first line still ends in sum=12.
If the first line ends in 8, the body of Add is a minus. A random draw does not repair that. Change return left - right; back to return left + right;. If the second line is not 2026-03-05, check the month and the pattern. Fix each line where it is written.
Keep one Pine. Keep the sum format without a width. Keep new DateTime(2026, 3, 5). Keep one new Random() and one Next(1, 7). Keep the bool check that prints inRange=True.
Summary stays shut
You can now print a sum you computed, a day you chose, and a number you did not choose, and prove that number stayed between 1 and 6. The next page, summary, gathers what Quality and Wrap-up built in the Pine file before the module goes on. This page stops before that review. It does not add a loop of rolls, a list of results, or a count of how often each face appears.
Run dotnet run once more. If the first line is left=10 right=2 sum=12, the second is 2026-03-05, the third is roll= with a number from 1 to 6, and the fourth is inRange=True, this lesson did its job.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top