2

In this tutorial in the part of Daily inspirations he says:

$quote = array(
1 => "Quote 1",
2 => "Quote 2",
3 => "Quote 3",
4 => "Quote 4",
5 => "Quote 5",
);
srand ((double) microtime() * 1000000);
$randnum = rand(1,5);
echo"$quote[$randnum]";

I do not understand what he is doing (literally) here:

srand ((double) microtime() * 1000000);

Could you please help me understand what this does?

I know srand() is to:

Seed the random number generator

But why does he do this, what's the point of it?

By the way: I would have gone with something like this:

<?php

$quotes = array(

"one",
"two",
"three"

);

echo $quotes[rand(0,count($quotes)-1)];
?>

Is there anything wrong with this?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Trufa
  • 39,971
  • 43
  • 126
  • 190
  • it's just a thingy about being really random or not... http://www.capuzza.com/detail.php?ID=115293 – acm Dec 06 '10 at 16:32
  • @andre matos that is from 2007 I looks that it is no really necessary any more: http://stackoverflow.com/questions/4368616/do-not-understand-php-daily-quote-tutorial/4368656#4368656 THanks!! – Trufa Dec 06 '10 at 16:37
  • You're right, but that was all I knew about it (really outdated here!!). That's why I posted as a comment and not as a real answer! I feel so much more enlightened now! :-) – acm Dec 06 '10 at 16:41

4 Answers4

5

Most random number generators aren't actually random; the next number they generate is calculated using a simple-yet-large function from previous numbers that have been generated. Seeding the RNG gives it a "previous" number to work from, since many times they start from the same known state each time.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
4

Before PHP 4.2 you needed to 'seed' the randomizer in order for it to actually be 'random.' Now it's pointless to seed the randomizer.

The docs state:

Note: As of PHP 4.2.0, there is no need to seed the random number generator with srand() or mt_srand() as this is now done automatically.

see: http://php.net/manual/en/function.srand.php

Your solution is just as effective.

Darryl E. Clarke
  • 7,537
  • 3
  • 26
  • 34
  • @Darryl E. Clarke, So, bottom line, no longer necessary! Thanks! – Trufa Dec 06 '10 at 16:35
  • 1
    With or without specify the seed, in any PHP version (or in any other language, for that matter) you will NEVER have random numbers, as they are always derived in a deterministic way from the seed. – nico Dec 06 '10 at 16:35
  • @nico TRUE! thanks! learned that here :) http://stackoverflow.com/questions/3956478/understanding-randomness – Trufa Dec 06 '10 at 16:38
  • @nico - agreed. But that's not really the topic here. – Darryl E. Clarke Dec 06 '10 at 16:39
  • 1
    @Darryl E. Clarke: sure, I was just pointing out (forgetting for a second about the differences between PHP versions) that the numbers generated after calling `srand` are no more or no less random than those generated without calling `srand`. In fact, the term pseudo-random should be preferred. – nico Dec 06 '10 at 16:44
  • and, by the way, as nobody seems to have linked it yet (what a shame!), here's the compulsory Dilbert comic: http://dilbert.com/strips/comic/2001-10-25/ – nico Dec 06 '10 at 16:50
2

The idea behind calling srand() is to provide "better" random numbers. But it is not necessarily true that calling srand() brings "more random" numbers. Since PHP 4.2 it is not essential to call srand() because PHP does this internal.

thedom
  • 2,498
  • 20
  • 26
2

This simply is an artifact from a twenty-year-old tutorial (use another one!):

  • The random number generator is seeded automatically already for a very long time
  • mt_rand should be preferred over rand, as it is faster and "more random" (i.e. it gives unbiased random variables in more degrees)

PS: I would recommend using array_rand here, because you don't need to care about the exact keys.

echo $quotes[array_rand($quotes)];
NikiC
  • 100,734
  • 37
  • 191
  • 225
  • Yep, knew it was old. Thanks for `mt_rand`!! – Trufa Dec 06 '10 at 17:04
  • @Trufa: @BTW: Your code is way better when theirs. The usage of a 0-indexed array is definitely better and using the more generic `length` is better too. But as noted in my post you could even make it more generic by using `array_rand` ;) – NikiC Dec 06 '10 at 17:06
  • that looks better and is a great short cut, thanks!! but I could not get what you ment by `you will need to change the 5 in the script` what would I have to change?? thanks again!! – Trufa Dec 06 '10 at 17:28