Answer the question
In order to leave comments, you need to log in
Is the problem in the "non-random" randomness or an implementation error?
I have a function that paints random pixels on a photo depending on the specified degree of "corruption" of the photo.
public Bitmap Break(float ratio)
{
if (ratio < 0 || ratio > 1)
{
throw new ArgumentException("Ratio must be in range 0..1");
}
var bitmap = new Bitmap(_path);
var width = bitmap.Size.Width;
var height = bitmap.Size.Height;
var pixelsToBrake = Math.Floor(width * height * ratio);
for (int i = 0; i <= pixelsToBrake; i++)
{
bitmap.SetPixel(new Random().Next(0, width), new Random().Next(0, height), Color.White);
}
return bitmap;
}
Answer the question
In order to leave comments, you need to log in
In C#, when a Random object is created without a parameter, it is initialized with the system time, which has finite resolution. If two objects are created with the same initializer value, they will produce the same sequence.
In your case, you need to create a Random object once before the loop, and only call its Next method in the loop.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question