Jump to content

poor random number generation in Add Noise filter


Recommended Posts

While usinng the Effects->Noise->Add Noise function, I was surprised to see a clear pattern of "squares" emerge from the noise. This is clearly the result of using a "bad" pseudo-random number generator (PRNG), such as System.Random, which is a simple linear generator.

 

While I haven't looked at the source yet, I assume System.Random is being used. A switch to a better PRNG would prevent any strange effects, and probably improve the appearance and performance of all filters and functions which utilize random numbers. Switching to a cryptographic RNG such asSecurity.Cryptography.RandomNumberGenerator is probably overkill and too slow for an imaging application, but that can be used to seed a more traditional (fast) PRNG.

 

There are many non-cryptographic RNGs with much better performance and distribution than System.Random if one is willing to pull in a small external library. The most popular "good" non-cryptographic RNG is "Mersenne Twister", with free C# implementations available all over the place. Wikipedia directs us to http://archive.msdn.microsoft.com/MersenneTwister for C# implementations, but Google shows many others with various licenses.

 

See attached PNG of noise being applied to a black background where you can see the "pattern of squares", especially near the top center of the image. It is most visible as bands of "more noise" running vertically down the image at regularly spaced intervals. This is in my experience a clear sign of a simple linear RNG being used.

 

Problem image generated with Add Noise effect on v3.5.11, Windows 7 x64, all framework and OS patches through 1/13/2014.

 

post-134862-0-18496200-1389626458_thumb.

Link to comment
Share on other sites

Sorry, I had to crop and it wasn't as clear after that. See teh following two attachments, one showing the add noise command, and then the same image after contrast adjustment showing the "pattern" more clearly.

 

The size of the "squares" appears to be about 75 pixels on a side with this example.

 

post-134862-0-10142300-1389670725_thumb.

Link to comment
Share on other sites

This is not a problem with the noise generation. It's a problem with resize rendering.

To reproduce:

Render noise. Any intensity works, but it's most visible at max intensity.

Zoom to something slightly off a factor of 100 (it was clearest for me at 51% and 98%. Also works with numbers just over 100%)

Also works, but not as well, with image>resize

No, Paint.NET is not spyware...but, installing it is an IQ test. ~BoltBait

Blend modes are like the filling in your sandwich. It's the filling that can change your experience of the sandwich. ~Ego Eram Reputo

Link to comment
Share on other sites

The "squares" pattern is quite visible at 100% pixel-for-pixel zoom, even outside of paint.NET in a browser, using this second test image I posted previously above:

http://forums.getpaint.net/uploads/monthly_01_2014/post-134862-0-38051500-1389670699.png

 

So it is not a problem with any resizing algorithm in Paint.NET or elsewhere.

 

To reproduce in Paint.NET v3.5.11, start with an 800x600 canvas, fill with black, then go to Effects->Noise->Add Noise. Choose intensity = 64, saturation = 100, coverage = 100.

 

I haven't tried on another machine yet, but it certainly looks like you are using a fixed seed for the random number generator, as it is reproducible for me when I re-use the same settings.

 

Assuming this portion of the code is still the same as the previous open-source v3.36 code, you are indeed using System.Random with a timestamp-based seed in this filter. This construction looks to me like it would always produce very non-random output. System.Random is known to have a short period and decidedly non-random output, see this MSFT bug for example.

Edited by rmalayter
Link to comment
Share on other sites

The "squares" pattern is quite visible at 100% pixel-for-pixel zoom, even outside of paint.NET in a browser, using this second test image I posted previously above:

http://forums.getpaint.net/uploads/monthly_01_2014/post-134862-0-38051500-1389670699.png

That is a definitely a Moiré pattern caused by viewing the image resized. Look at the way the pixels appear smeared in the middle of the squares; that's caused by a resize, not by a predictable RNG. If it was at 100% in Paint.NET, then your browser is resizing it. Press Ctrl+0 to reset your browser to 100% zoom.

xZYt6wl.png

ambigram signature by Kemaru

[i write plugins and stuff]

If you like a post, upvote it!

Link to comment
Share on other sites

When you save your image to share with us, are you saving it straight out of paint.net (ctrl+s) or are you taking a screenshot?

I followed your steps to the letter (it wasn't hard...all default settings) and here's what I have:

EDIT: just kidding...photobucket converted to a jpeg and now it's covered in jpeg artifacts

EDIT 2: try this link http://s758.photobucket.com/user/pdnnoob/media/Untitled-10.png.html

No, Paint.NET is not spyware...but, installing it is an IQ test. ~BoltBait

Blend modes are like the filling in your sandwich. It's the filling that can change your experience of the sandwich. ~Ego Eram Reputo

Link to comment
Share on other sites

Agreed, I am only able to repro by resizing.

 

The Doctor: There was a goblin, or a trickster, or a warrior... A nameless, terrible thing, soaked in the blood of a billion galaxies. The most feared being in all the cosmos. And nothing could stop it, or hold it, or reason with it. One day it would just drop out of the sky and tear down your world.
Amy: But how did it end up in there?
The Doctor: You know fairy tales. A good wizard tricked it.
River Song: I hate good wizards in fairy tales; they always turn out to be him.

Link to comment
Share on other sites

I tried too , everything - no joy. Could not reproduce the results. I think you've created a beat frequency by changing sampling rate (resolution) and a have mistaken it for predictability. 

You seem like a smart man , ask yourself this question: "What are the odds that I would, purley by chance,  find a repeating pattern that's a perfect harmanoic of my image size (height and width)?"

Edited by TechnoRobbo

Go out there and be amazing. Have Fun, TR
TRsSig.png?raw=1
Some Pretty Pictures Some Cool Plugins

Link to comment
Share on other sites

Dammit, now that I am in the office I cannot reproduce. I did use a screenshot-to-PNG for the images, but made sure that zoom was set to 100% in paint.net first. Maybe I have some weird monitor scaling going on with my laptop when it is undocked. Have to mess with the icky HP display driver to make sure that isn't the case.

 

I have seen scatterplots from "synthetic" data generated by poor RNGs that clearly show diagonal or rectangular regularites, and that's what I guessed was happening here. But I am flummoxed as to why I cannot reproduce today, as was able to do so repeatedly yesterday while my machine was undocked.

 

So... nevermind. Thank you all for your help, and I promise to have a dead-bang, 100% repro case if I bring this one up again.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...