# Noise Generator in C#

Actually random generator in C# is so good, that we don’t need additional algorithms to make noise generator in C#. On the other hand, it is useful to have efficient algorithm to generate noise, not necessarily in visual studio. In this example, we will generate two-channel noise (two independent noises, both in amplitude range from 0 to 1). For each channel we need three numbers “seed”, each in range from 0 to 0.5. Let’s denote them with a1, b1 and c1 for channel 1 and a2, b2, c2 for channel 2. We can give them fixed values, or use local random number generator to make initial “seeds”.

Random rnd1 = new Random();
int int_s1 = rnd1.Next(0, 1001); // creates a number between 0 and 1000
int random1, random2;

double dbl_s1 = (double)int_s1 / 1000.0; // Creates double number between 0 and 1
double a1, b1, c1, a2, b2, c2, d1, d2;

for (int i = 1; i < 7; i++)
{

int_s1 = rnd1.Next(0, 1001); // creates a number between 1 and 1000

dbl_s1 = (double)int_s1 / 1000.0; // Creates double number between 0 and 1

dbl_s1 /= 2.0; // Creates double number between 0 and 0.5

if (i == 1) a1 = dbl_s1;

if (i == 2) b1 = dbl_s1;

if (i == 3) c1 = dbl_s1;

if (i == 4) a2 = dbl_s1;

if (i == 5) b2 = dbl_s1;

if (i == 6) c2 = dbl_s1;

}

We are ready to generate first “noisy” numbers d1 and d2 (for corresponding channels).

d1 = 13.0 * a1 + 163.0 * b1 + c1 – Math.Floor(13.0 * a1 + 163.0 * b1 + c1);
d2 = 13.0 * a2 + 163.0 * b2 + c2 – Math.Floor(13.0 * a2 + 163.0 * b2 + c2);

Then make as many random numbers as you want. They will be in range from 0-1. We can turn them into any other range by simply subtract 0.5 and multiply with 2.0. For example:

n1=0.5*delta1*2.0*(d1 – 0.5);
n2=0.5*delta2*2.0*(d2 – 0.5);

We can present quality of the noise generated (uniform distribution and non-correlation) by showing them into MSChart control and in picture box. For picture box, we rescale random numbers from range 0-1 to 0 to pictureBox1.Size.Width or pictureBox1.Size.Height

for (int i = 0; i < 1001; i++)
{

a1 = b1;

b1 = c1;

c1 = d1;

d1 = 13.0 * a1 + 163.0 * b1 + c1 – Math.Floor(13.0 * a1 + 163.0 * b1 + c1);

random1 = (int)(d1*(double)pictureBox1.Size.Width + 1.0);

a2 = b2;

b2 = c2;

c2 = d2;

d2 = 13.0 * a2 + 163.0 * b2 + c2 – Math.Floor(13.0 * a2 + 163.0 * b2 + c2);