Page 4 of 5

Re: Here is a reason why not to use rand().

Posted: Wed Dec 31, 2008 2:42 pm
by josh
pytrin wrote:random as far as modern science can stipulate
Random as far as humans can stipulate, the matter of the fact is the phenomena you are referring to is probably the double slit experiment, where if we look for a wave we get an interference pattern, if we look for a particle we get a particle.. All that proves is that by observing it we changed it, but the matter of the fact is its still a philosophical debate whether or not that "act of observing", the electrical impulse in our brain that caused us to carry out the experiment, was in fact itself random or not. Its not random because the pattern is is that every time we observe it its a re-action to our action, we caused it to behave that way.. doesn't mean its random.

If everytime we observe a particle it is in a place that appears random to us, that is not proof randomness exists. The matter of the fact is randomness is like beauty, it is not a tangible concept, its a human emotion which evolved to prevent us from wasting time looking for things we won't find.

Re: Here is a reason why not to use rand().

Posted: Wed Dec 31, 2008 2:44 pm
by kaisellgren
pytrin wrote:
Science is truth until elseway proven. So far almost everything is proven to be wrong later.
Don't know how you reached that conclusion. Much of science has been going through degrees of accuracy, but good science (ie, theories backed by physical evidence) is generally not wrong. Newtonian mechanics was not "wrong", it was inaccurate in speeds approaching the speed of light, so it needed an adjustment - the special theory of relativity. They both converged to the same results in slower speeds. Then quantum mechanics explained previously unknown phenomenon, and provide the basis for many other branches of physics such as thermodynamics. The theory itself might not be 100% correct, but its observable phenomona (such as random location for subatomic particles) is empirical fact that can not be brushed away by saying you don't believe in it.
Okay you know what. This is really pointless. I doubt people come to here to read about physics...

I do not base my thoughts just on opinions. I do have a high educated friend - good one, who is a physics professor at the univery of Helsinki. He has always been right about everything I have thrown at him, so I believe in him a lot. I have spoken about nearly everything including about random and randomness. I have decided what is my side and I will stay on it unless I am very confident about being wrong. If I'm wrong, I would not really care less. I have actually never been to school myself, I was born in north in the cold areas where there were no schools and I have right now no education what-so-ever. Probably there are lots of things I am wrong about since I have based all my knowledge on resources on the Internet. I can cook thanks to the Internet, I can read and write thanks to my mum and the Internet. I can also speak some English thanks to the Internet. Also coding coding, maths, etc are all learnt from the Internet. Due to all these reasons I won't continue discussing about physics, philosophy, etc. Not because I am not interested, but because I have no education and not sufficient English terminology to speak from myself. Thanks for the discussion though.

From now on I'll only reply to this thread related discussions.

Re: Here is a reason why not to use rand().

Posted: Wed Dec 31, 2008 2:48 pm
by josh
God may not roll dice but does he play poker? :-D
-Hawking

Re: Here is a reason why not to use rand().

Posted: Wed Dec 31, 2008 2:51 pm
by Eran
but the matter of the fact is its still a philosophical debate whether or not that "act of observing"
The double-slit is just a famous simple experience to show in a simple way the effects of quantum mechanics, but it is not what I was referring to. Many branches of science and technologies today rely on the non-deterministic property of subatomic particle. I'm not a philosopher, but amongst scientists the debate of whether physical observation can affect reality has been long since decided. The amount of piling empirical evidence is just to great to ignore. Every new experiment attempting to disprove this hard to digest aspect of QM just ends up proving it even further.

There are even technologies that rely specifically on collapsing wave-functions through observation. This is not some philosophical debate on human conciseness or something of the like.
I do not base my thoughts just on opinions. I do have a high educated friend - good one, who is a physics professor at the univery of Helsinki. He has always been right about everything I have thrown at him, so I believe in him a lot. I have spoken about nearly everything including about random and randomness. I have decided what is my side and I will stay on it
Sorry, but I think you misunderstood him. I didn't mean to make a big deal of this, but you are basing your opinions on misinterpretation and basically decreeing modern science in the name of your believes which to me is semi-offensive. But I will drag this on no more. Peace.

Re: Here is a reason why not to use rand().

Posted: Wed Dec 31, 2008 3:31 pm
by josh
pytrin wrote: The amount of piling empirical evidence is just to great to ignore. Every new experiment attempting to disprove this hard to digest aspect of QM just ends up proving it even further.
Im not trying to disprove QM, you are misunderstanding. I'm saying no one has proven wether or not thought ( or anything ), operates according to laws of physics, or wether we have free will, "randomness" as some would say. So far every scientific measurement has shown things operate according to laws, albeit we get "impure" results at a quantum level, noone has shown this to be due to "randomness". Its funny how heated the discussions here can get, yet the people in them continue them. Its like deep down we know we need eachother to challenge our thoughts because each thought in isolation is inherently flawed. Like how the 'nuerons' in a neural network work together & fluctuate to form patterns that react the their inputs

Re: Here is a reason why not to use rand().

Posted: Wed Dec 31, 2008 4:38 pm
by onion2k
This isn't really the place to be discussing the nature of randomness and it's implementation on current computers. Can we get back on topic here please?

I might split the physics stuff into a separate topic in General Discussion later. Not tonight though because it's NYE and not even I'm that dismal..

Re: Here is a reason why not to use rand().

Posted: Thu Jan 01, 2009 5:24 pm
by Syntac
I've tested this myself. There doesn't seem to be much of a difference between rand() and mt_rand() (on my machine at least), although I suppose it's better to use mt_rand() anyway.

Re: Here is a reason why not to use rand().

Posted: Thu Jan 01, 2009 6:15 pm
by josh
As a rule of thumb I would use mt_rand, if you're making repeated calls to mt_rand that is slowing down performance then thats indication it's time to downgrade to rand.

Re: Here is a reason why not to use rand().

Posted: Fri Jan 02, 2009 5:16 am
by VladSun
I'm sorry if one of you had already written it (I didn't have the time to read the whole thread carefully), but isn't it better to calculate the entropy of the generated sequences by both functions? Or the autocorrelation?

Re: Here is a reason why not to use rand().

Posted: Fri Jan 02, 2009 7:40 am
by VladSun
Interesting:

Code: Select all

define('M', 500);
 
function hystogram($f)
{
    $a = array();
    for ($i=0; $i < M; $i++)
    {
        $c = call_user_func($f, $i);
        
        if (!isset($a[$c]))
            $a[$c] = 1;
        else
            $a[$c] ++;
    }
 
    $norm = 0;
        
    for ($i=0; $i < M; $i++)
    {
        if (!isset($a[$i]))
            $a[$i] = 0;
            
        if ($norm < $a[$i])
            $norm = $a[$i];
    }
    
    for ($i=0; $i < M; $i++)
        $a[$i] /= $norm;
    
    sort($a);   
    
    return $a;
}
 
function _rand($x)
{
    return rand(0, M);
}
 
function _mt_rand($x)
{
    return mt_rand(0, M);
}
 
header('Content-Type: image/png');
 
$im = imagecreatetruecolor(M, M + 20);
 
$r = hystogram('_rand');
$r2 = hystogram('_mt_rand');
 
for ($i=0; $i < M; $i++)
{
    imagesetpixel( $im, $i, M - M*$r[$i] - 10, imagecolorallocate( $im, 255,255,255));
    imagesetpixel( $im, $i, M - M*$r2[$i] - 10, imagecolorallocate( $im, 255,0,0));
}
    
imagepng($im);
imagedestroy($im);
What I expected was to see uniformly distributed values, but not this!

Re: Here is a reason why not to use rand().

Posted: Fri Jan 02, 2009 7:58 am
by VladSun
Another, nicer graph. I've changed a little bit of my previous code:

Code: Select all

 
define('M', 500);
define('U', 100000);
 
function hystogram($f)
{
    $a = array();
    for ($i=0; $i < U; $i++)
    {
        $c = call_user_func($f, $i);
        
        if (!isset($a[$c]))
            $a[$c] = 1;
        else
            $a[$c] ++;
    }
 
    $norm = 0;
        
    for ($i=0; $i < M; $i++)
    {
        if (!isset($a[$i]))
            $a[$i] = 0;
            
        if ($norm < $a[$i])
            $norm = $a[$i];
    }
    
    for ($i=0; $i < M; $i++)
        $a[$i] /= $norm;
    
    sort($a);   
    
    return $a;
}
 
function _rand($x)
{
    return rand(0, M);
}
 
function _mt_rand($x)
{
    return mt_rand(0, M);
}
 
header('Content-Type: image/png');
 
$im = imagecreatetruecolor(M, M + 20);
 
$r = hystogram('_rand');
$r2 = hystogram('_mt_rand');
 
for ($i=0; $i < M; $i++)
{
    imagesetpixel( $im, $i, M - M*$r[$i] , imagecolorallocate( $im, 255,255,255));
    imagesetpixel( $im, $i, M - M*$r2[$i], imagecolorallocate( $im, 255,0,0));
}
    
imagepng($im);
imagedestroy($im);
 

Re: Here is a reason why not to use rand().

Posted: Fri Jan 02, 2009 8:24 am
by kaisellgren
I love them VladSun :)

I would like to see images generated with /dev/urandom so can you make one since you are not using Windows, are you?

Re: Here is a reason why not to use rand().

Posted: Fri Jan 02, 2009 8:46 am
by josh
The entire idea behind calculus is you can transform data in ways in which it is easier for humans to detect patterns. Formally defined the patterns may be ad-hoc, pages long, and incomprehensible and practically useless, but there are still definable patterns there. My theory is that entropy is subjective. Depending on the observer, it is relative. If I was so inclined I could take data sets from the real world that are "random" by the lay definition, and generate charts like you guys are generating that show correlations, its just a matter of devising metrics to construe the data in desired forms. Basically it just goes back to what I was saying that nothing is "random", but does it really matter for practical purposes? No... these histograms were created ad hod just to show these patterns. The findings are useful qualitatively but not quantitatively. From this we can conclude mt_rand is less predictable when compared with rand, thats about it. The charts are very cool though, I'll probably play around with your code Vlad.

My point is simply that these are visualization techniques, and do not necessarily express any defects inherit to either function.

Re: Here is a reason why not to use rand().

Posted: Fri Jan 02, 2009 9:03 am
by kaisellgren
jshpro2 wrote:The entire idea behind calculus is you can transform data in ways in which it is easier for humans to detect patterns. Formally defined the patterns may be ad-hoc, pages long, and incomprehensible and practically useless, but there are still definable patterns there. My theory is that entropy is subjective. Depending on the observer, it is relative. If I was so inclined I could take data sets from the real world that are "random" by the lay definition, and generate charts like you guys are generating that show correlations, its just a matter of devising metrics to construe the data in desired forms. Basically it just goes back to what I was saying that nothing is "random", but does it really matter for practical purposes? No... these histograms were created ad hod just to show these patterns. The findings are useful qualitatively but not quantitatively. From this we can conclude mt_rand is less predictable when compared with rand, thats about it. The charts are very cool though, I'll probably play around with your code Vlad.

My point is simply that these are visualization techniques, and do not necessarily express any defects inherit to either function.
I second that.

I actually got quite inspirated by the graph above and I started breaking the Mega Upload CAPTCHA. 8)

Re: Here is a reason why not to use rand().

Posted: Fri Jan 02, 2009 1:53 pm
by VladSun
jshpro2 wrote:The entire idea behind calculus is you can transform data in ways in which it is easier for humans to detect patterns. Formally defined the patterns may be ad-hoc, pages long, and incomprehensible and practically useless, but there are still definable patterns there. My theory is that entropy is subjective. Depending on the observer, it is relative. If I was so inclined I could take data sets from the real world that are "random" by the lay definition, and generate charts like you guys are generating that show correlations, its just a matter of devising metrics to construe the data in desired forms. Basically it just goes back to what I was saying that nothing is "random", but does it really matter for practical purposes? No... these histograms were created ad hod just to show these patterns. The findings are useful qualitatively but not quantitatively. From this we can conclude mt_rand is less predictable when compared with rand, thats about it. The charts are very cool though, I'll probably play around with your code Vlad.

My point is simply that these are visualization techniques, and do not necessarily express any defects inherit to either function.
You know that we have maximum entropy with pure random signal (chaos) and minimum entropy with fully predictable signal.
Pure random signal with N-levels is a one who has 1/N probability per state. That's why i expected that the histogram will be a straight line.
A fully predictable signal doesn't carry any information - e.g. a signal that has only a DC component.

My point is, that these charts are not only visual presentation. I don't search for a pattern here - I estimate the entropy, which is the randomness of a 1D signal. One way to do it is to check the probability distribution function of the signal (the histogram). It's not subjective - only mathematics are applied.