Random numbers in PHP

cursedpsp

New Member
Messages
237
Reaction score
0
Points
0
Hi, ill show you how to use 'mt_rand' 'srand' and a really complicated one that makes hexadecimal numbers.

lets start off with the 'mt_rand' function.
Code:
//Minimum number
$min = 1;
 
//Maximum number
$max = 100;
 
//Put mt_rand into a variable
$random = mt_rand($min, $max);
 
echo "Our random number of the day is: " . $random;

Now for the 'srand' function.
Code:
//Minimum number
$min = 1;
 
//Maximum number
$max = 100;
 
srand((double) microtime()*1000003);
//1000003 because it is a prime number
 
//convert it to a variable
$random = rand($min, $max);
 
echo "Another random number today is: " . $random
I dont actually know what the difference it to the random number functions but they do their job :).

The next one will create a hexadecimal code which is very useful for sessions and cookies, so that people will never guess them!
Code:
$hex = sha1(uniqid(mt_rand()));
 
echo "Todays impossible to guess hex code is: " . $hex;

echo
 

cursedpsp

New Member
Messages
237
Reaction score
0
Points
0
There is a way to increase the hex digits but i dont know how to - anyone?
 

hamsn

New Member
Messages
290
Reaction score
0
Points
0
where will this be useful?
i mean like can we use it in visitor counter
no. of seconds logged in


or else...ideas?
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
I don't think this is really a good tutorial to be honest...
mt_rand() is just a function like rand(), you need 30 seconds to look it up at php.net...
And about the second srand() and rand() thing...
http://be.php.net/manual/en/function.srand.php
"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."
So you don't need to call srand() at all...
And sha1() is just a form of encoding stuff, using the secure hash algorithm 1, it has nothing to do with random numbers...

Sorry, but if you make a tutorial, please inform yourself properly...
 

cursedpsp

New Member
Messages
237
Reaction score
0
Points
0
where will this be useful?
i mean like can we use it in visitor counter
no. of seconds logged in


or else...ideas?

Random advertisement,

Those "random joke of the day" things use it.

Anything that you want to be random
 

cursedpsp

New Member
Messages
237
Reaction score
0
Points
0
Blimey, i found this atlast - i was searching for days for this (even using the search bar)
 
Top