srand? #8

Closed
adwell opened this Issue Dec 28, 2012 · 10 comments

Comments

Projects
None yet
5 participants

adwell commented Dec 28, 2012

Can you remove the call to srand? That gives me the willies. Thanks!

Owner

kgn commented Jan 10, 2013

Do you have a suggestion for what could be used instead? I used srand instead of arc4random so the number can be seeded so the noise is consistent and draws the same way every time.

adwell commented Jan 15, 2013

Well, we can't use srand because that will break apps that rely on randomness. I take it you want the randomness to be the same across all runs? The best approach would probably be to implement your own (simple) pseudo-random number function and seed it with a known value. I think this is actually pretty easy, especially since this isn't for security purposes:

http://en.wikipedia.org/wiki/List_of_random_number_generators
http://en.wikipedia.org/wiki/Linear_congruential_generator
http://www.mscs.dal.ca/~selinger/random/

what about arc4random() ?

kgn wants a random pattern that's always the same. arc4random() won't help, I think. The easiest thing would be to either store a random array or just write a tiny random number generator. It could be done in a few lines. Here's the code:

http://rosettacode.org/wiki/Linear_congruential_generator

If you insist on keeping the existing pattern, just hardcode the 128x128 array of random numbers generated after srand(115).

Owner

kgn commented Feb 11, 2013

@tonymillion @gurgeous @adwell yep I didn't use arc4random() because it cannot be seeded. Adding a 16384 length array to the code sounds awful :) Added a random number generator is something I'd considered doing before and seems like the best solution to this issue.

Ah yes, I see why arc4 would be useless in this case.

I kind of agree with the "put the precomputed value in the code" (it could always be a separate file thats #imported later). It does keep things clean and wont break if the underlying srand() / rand() start working differently!

Owner

kgn commented Feb 15, 2013

I've tried to implement the rand function from Rosetta Code but it's not very random, I'm sure I'd messed something up. Suggestions/patches welcome: https://github.com/kgn/KGNoise/tree/dev/rand
Screen Shot 2013-02-14 at 10 43 12 PM

Contributor

raymondjavaxx commented Feb 15, 2013

@kgn try changing the type of kgnoise_rseed from char to int. That gave me better results.

ss

Owner

kgn commented Feb 15, 2013

@raymondjavaxx ah yeah, great!

@kgn kgn closed this in ad2d559 Feb 15, 2013

Great, thanks guys!

rastersize added a commit to rastersize/KGNoise that referenced this issue Jun 4, 2013

Merge branch 'master' of https://github.com/kgn/KGNoise
* 'master' of https://github.com/kgn/KGNoise: (22 commits)
  Updating syntax highlighting
  removed enum as it is no longer needed
  Add explicit type cast and curly braces to appease compiler
  typedef KGLinearGradientDirection enum to avoid multiple definition
  switching to instancetype
  Rename "rgba" to "bitmapData"
  Set KGNoiseView's default contentMode to UIViewContentModeRedraw
  Add autoResizing support to the iOS example
  Updated readme: info about the image categories
  Added the image noise categories to the main KGNoise file so there is only one thing to add. Also implemented the image category for NSImage and added examples to the sample apps.
  Removed rand and srand fix #8
  static inline instead of duplicate method declaration
  rename the kImageSize const
  Fix a warning
  switch to NS_ENUM
  Updated readme to reflect direction property on linear gradient view
  Code to allow specification of gradient direction.
  Add 'static' to private function to avoid "Missing Function Prototypes" warnings.
  Added a mask to the image category
  Make sure both categories play nice with Retina displays
  ...

Signed-off-by: Aron Cedercrantz <aron@cedercrantz.se>

Conflicts:
	KGNoise.m

rastersize added a commit to rastersize/KGNoise that referenced this issue Jun 4, 2013

Merge branch 'master' into integration
* master: (22 commits)
  Updating syntax highlighting
  removed enum as it is no longer needed
  Add explicit type cast and curly braces to appease compiler
  typedef KGLinearGradientDirection enum to avoid multiple definition
  switching to instancetype
  Rename "rgba" to "bitmapData"
  Set KGNoiseView's default contentMode to UIViewContentModeRedraw
  Add autoResizing support to the iOS example
  Updated readme: info about the image categories
  Added the image noise categories to the main KGNoise file so there is only one thing to add. Also implemented the image category for NSImage and added examples to the sample apps.
  Removed rand and srand fix #8
  static inline instead of duplicate method declaration
  rename the kImageSize const
  Fix a warning
  switch to NS_ENUM
  Updated readme to reflect direction property on linear gradient view
  Code to allow specification of gradient direction.
  Add 'static' to private function to avoid "Missing Function Prototypes" warnings.
  Added a mask to the image category
  Make sure both categories play nice with Retina displays
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment