/* the author and owner of this blog hereby allows anyone to test the security of this blog (on HTTP level only, the server is not mine, so let's leave it alone ;>), and try to break in (including successful breaks) without any consequences of any kind (DoS attacks are an exception here) ... I'll add that I planted in some places funny photos of some kittens, there are 7 of them right now, so have fun looking for them ;> let me know if You find them all, I'll add some congratz message or sth ;> */

Today I'll write about an interesting mistake (or misinterpretation in this case) I've spoted in my friends code, and also I'll mention a certain link I found in the referers. I'll start with the link...

Cool :)To tell you the truth I've experimented with 3D (long before the 2D version was created), but unfortunately I've chose wrong (ultra boring) files for tests, which made me cancel the project in the end. I'm really glad that irid didn't make the same mistake - some files look really great in 3D!

An interesting misinterpretation related to rand()Recently I've seen some code created by one of my friends, which I'll quote (the code, not the friend; it won't be a direct code, but nothing important is changed):

Curious about what was this piece of code supposed to do (since I expected it didn't do what was expected) I have asked my friend for explanation - he replied with a direct quote from the manual:

The rand() function returns a pseudo-random integer in the range [0, RAND_MAX].

So the above code was supposed to work like this: "I want rand() should return a value from 0 to 60000, so I'll change RAND_MAX to 60000". I must admit that that logic was undeniable.However, even if logically it was correct, it didn't work as it should. The reason? RAND_MAX is kinda "read-only", or, to be more precise, it is a constant declared by the developer of rand() which describes the maximal capabilities of rand() function (as in - the maximum value rand() can return). The value of this constant depends on the implementation, but the implementation does not use it. It's just an information - so, changing RAND_MAX in fact changes nothing.

Of course, the correct usage of rand() in this case is rand_value = rand() % (my_rand_limit + 1).

But what's interesting, is that you can make the above code work anyway! You just have to make the implementation, or the invocation, RAND_MAX related. You can do the later one by a short preprocessor directive:

#define rand() (rand() % RAND_MAX)

My friend has told me that he had tested the code earlier, and it has returned only values from the expected boundaries - so I began searching for the above #define in headers of different compilers, but I found nothing. As we figured out later, the test results were positive due to the my_rand_limit being very close to RAND_MAX, so the values seemed OK, and were accepted without further testing.

And that would be all for now :)

Comments:

Well, that would not be entirely correct. If you want to avoid skewing the random distribution, you should throw away any values greater than the highest multiple of (my_rand_limit+1) that fits in RAND_MAX. A simple if-condition would do.

The chance of getting a 0 is 3/5, while the chance of getting a 1 is 2/5. The effect of this will be less obvious when the RAND_MAX is many orders of magnitude larger than my_rand_max, but it would possibly skew your results.