5

I've been trying to wrap my head around solving this issue but I can't seem to find a simple solution to it.

I have a dynamic grid layout, articles are placed on the grid but have different width & height. This width & height of these different articles can change upon visiting the site but only when there is a new article added.

The issue is I have logic that uses mt_rand to generate random numbers within a range (to fit in the layout grid), is there a way I can pass in a seed (planning to use the newest article id) so that the random numbers are consistent until a new article is introduced? I wanted to use mt_srand but there doesn't seem to be an easy way to limit the random number within a range.

Thanks.

David Nguyen
  • 8,368
  • 2
  • 33
  • 49
  • this should answer your question looks like the same thing. http://stackoverflow.com/questions/5694319/how-random-is-phps-shuffle-function – Sinan Jun 01 '11 at 21:50
  • @Sinan, not a dupe of **that** question. – Johan Jun 01 '11 at 21:56
  • possible duplicate of [Generating UNIQUE Random Numbers within a range - PHP](http://stackoverflow.com/questions/5612656/generating-unique-random-numbers-within-a-range-php) – Johan Jun 01 '11 at 21:57
  • sorry guys I totally misread mt_srand I thought it was used to seed and generate a number – David Nguyen Jun 01 '11 at 22:06

1 Answers1

5

mt_srand() is used to set your seed and mt_rand() takes in a min and max if you want to set the range. Basically something like:

mt_srand($seed);
mt_rand($min, $max);

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.

rafi
  • 1,648
  • 2
  • 18
  • 23
Greg Field
  • 469
  • 3
  • 7