Class RandomExtensions
- Namespace
- AiDotNet.Extensions
- Assembly
- AiDotNet.dll
Provides extension methods for the Random class to generate numbers with specific distributions.
public static class RandomExtensions
- Inheritance
-
RandomExtensions
- Inherited Members
Remarks
For Beginners: This class adds new capabilities to .NET's built-in Random class. While the standard Random class can generate uniform random numbers (where every value has an equal chance), AI and machine learning often need different types of random numbers that follow specific patterns or distributions.
Methods
NextGaussian(Random)
Generates a random number from a Gaussian (normal) distribution with mean 0 and standard deviation 1.
public static double NextGaussian(this Random random)
Parameters
randomRandomThe Random object to extend.
Returns
- double
A random double value from a standard normal distribution.
Remarks
For Beginners: While regular random numbers (from Random.NextDouble()) give you any value between 0 and 1 with equal probability, Gaussian random numbers follow the famous "bell curve" pattern.
In a Gaussian distribution:
- Values near the mean (0 in this case) are more likely to occur
- Values far from the mean are less likely to occur
- The standard deviation (1 in this case) controls how spread out the values are
This method uses the Box-Muller transform, a mathematical technique to convert uniform random numbers into Gaussian random numbers.
Gaussian distributions are extremely important in AI and machine learning because:
- Many natural phenomena follow this distribution
- They're used to initialize weights in neural networks
- They're used in many algorithms like Gaussian processes and variational autoencoders
- They're essential for adding noise in techniques like simulated annealing
If you need a Gaussian distribution with a different mean and standard deviation,
you can transform the result: mean + standardDeviation * NextGaussian()
NextGaussian(Random, double, double)
Generates a random number from a Gaussian (normal) distribution with specified mean and standard deviation.
public static double NextGaussian(this Random random, double mean, double stdDev)
Parameters
randomRandomThe Random object to extend.
meandoubleThe mean (center) of the distribution.
stdDevdoubleThe standard deviation (spread) of the distribution.
Returns
- double
A random double value from the specified normal distribution.
Remarks
For Beginners: This is the same as NextGaussian() but allows you to specify where the bell curve is centered (mean) and how wide it is (standard deviation).