PDA

View Full Version : Math.random


Dalziel
04-29-2003, 11:59 AM
What is the PHP equivalent to Javascript math.random()

raf
04-29-2003, 12:14 PM
Not so good with Javascript, but to generate random numbers (suppose that's what you need), i use rand() or mt_rand()
http://www.php.net/manual/tw/function.rand.php

missing-score
04-29-2003, 03:37 PM
I have never used the rand() function to generate numbers (dunno why) but I believe you can also set a range:


<?php
rand(0,100); // should generate a number between 0 and 100.
?>

duniyadnd
04-30-2003, 08:37 AM
For your site, you can use rand() to generate random quotes from a text file etc.

And yes missing-score, that's how its done to generate a range.

Duniyadnd

Dalziel
04-30-2003, 12:01 PM
Originally posted by missing-score
I have never used the rand() function to generate numbers (dunno why) but I believe you can also set a range:


<?php
rand(0,100); // should generate a number between 0 and 100.
?>


All Javascript math.random does is generate a random decimal number between 1 and 0, so rand(1,100) is far better IMHO. To do that with Javascript you'd need something like this:

Math.round(Math.random() * 100);

missing-score
04-30-2003, 05:59 PM
Just as a subnote:

If you need a UNIQUE number, you could use:

uniqid("");

If you were looking for a hard to find code, (It would use letters) you could do:

md5(uniqid(rand(0,9999), 1)));