Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C# Dictionary

  1. Home
  2. C# Dictionary
  3. Math.PI / Math.E / Random

Math.PI / Math.E / Random

Classes for mathematical constants and random number generation. The Math class provides mathematical constants and functions, while the Random class generates random numbers.

Syntax

// Referencing mathematical constants
double pi = Math.PI;
double e  = Math.E;

// Creating a Random instance and getting random values
Random rng = new Random();
int    n   = rng.Next(min, max);     // An integer >= min and < max
double d   = rng.NextDouble();       // A floating-point number >= 0.0 and < 1.0

Member List

MemberDescription
Math.PIA constant representing the ratio of a circle's circumference to its diameter, π (3.141592653589793).
Math.EA constant representing the base of the natural logarithm, e (2.718281828459045).
Math.Abs(x)Returns the absolute value of a number.
Math.Sqrt(x)Returns the square root of a number.
Math.Pow(x, y)Returns x raised to the power of y.
Math.Round(x, n)Returns the value rounded to n decimal places (banker's rounding).
Math.Floor(x)Returns the largest integer less than or equal to x (floor function).
Math.Ceiling(x)Returns the smallest integer greater than or equal to x (ceiling function).
Math.Max(a, b)Returns the larger of two values.
Math.Min(a, b)Returns the smaller of two values.
Random.Next(min, max)Returns a random integer that is >= min and < max.
Random.NextDouble()Returns a random floating-point number that is >= 0.0 and < 1.0.
Random.NextBytes(buf)Fills a byte array with random values.
Random.SharedA thread-safe shared instance available in .NET 6 and later.

Sample Code

using System;

// Referencing mathematical constants
Console.WriteLine($"π = {Math.PI}");   // 3.141592653589793
Console.WriteLine($"e = {Math.E}");    // 2.718281828459045

// Calculate the area of a circle (π × r²).
double radius = 5.0;
double area = Math.PI * Math.Pow(radius, 2);
Console.WriteLine($"Area of circle with radius {radius}: {area:F4}"); // 78.5398

// Basic usage of math functions
Console.WriteLine(Math.Sqrt(16));        // 4  — square root
Console.WriteLine(Math.Abs(-42));        // 42 — absolute value
Console.WriteLine(Math.Max(10, 20));     // 20 — maximum value
Console.WriteLine(Math.Round(3.456, 2)); // 3.46 — rounded

// Generate random numbers with Random (no seed means truly random).
Random rng = new Random();
Console.WriteLine(rng.Next(1, 7));     // A dice roll from 1 to 6
Console.WriteLine(rng.NextDouble());   // A value >= 0.0 and < 1.0

// Passing a fixed seed produces the same sequence every time (useful for testing).
Random seeded = new Random(42);
Console.WriteLine(seeded.Next(1, 101)); // Always the same value

// In .NET 6 and later, use Random.Shared for thread-safe access.
int dice = Random.Shared.Next(1, 7);
Console.WriteLine($"Dice: {dice}");

Notes

The Math class is a static class and cannot be instantiated. You reference its constants directly, such as Math.PI and Math.E.

The Random class can generate a reproducible sequence of random numbers by passing a seed (an integer) to its constructor, which makes it useful in test code and learning materials. Sharing a single Random instance across multiple threads in a multithreaded environment can cause race conditions. In .NET 6 and later, use Random.Shared to access a thread-safe shared instance.

Random numbers are also commonly combined with asynchronous operations such as Task.Run() / Task.Delay(). For more on numeric operations, see Enumerable.Aggregate().

If you find any errors or copyright issues, please .