x10Hosting Forums

Corporate Free Hosting for the Masses.



Register

Reply
 
LinkBack Thread Tools Display Modes
x10 Sophmore

Join Date: Apr 2008
Posts: 205
Credits: 2,574
cursedpsp is on a distinguished road
Location: Wiltshire, England

Quote  
05-05-2008, 09:47 AM
Random numbers in PHP

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
Reply With Quote
cursedpsp is offlineReport Post
x10 Sophmore

Join Date: Apr 2008
Posts: 205
Credits: 2,574
cursedpsp is on a distinguished road
Location: Wiltshire, England

Quote  
05-26-2008, 06:52 AM
Re: Random numbers in PHP

There is a way to increase the hex digits but i dont know how to - anyone?
Reply With Quote
cursedpsp is offlineReport Post
x10 Sophmore

hamsn's Avatar

Join Date: Mar 2008
Posts: 155
Credits: 2,745
hamsn is on a distinguished road
Location: Global

Send a message via AIM to hamsn Send a message via MSN to hamsn Send a message via Yahoo to hamsn
Quote  
05-27-2008, 03:04 AM
Re: Random numbers in PHP

where will this be useful?
i mean like can we use it in visitor counter
no. of seconds logged in


or else...ideas?
Reply With Quote
hamsn is offlineReport Post
x10 Lieutenant

marshian's Avatar

Join Date: Jan 2008
Posts: 392
Credits: 4,983
marshian is a glorious beacon of light
Location: Belgium

Send a message via MSN to marshian
Quote  
05-27-2008, 12:14 PM
Re: Random numbers in PHP

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...
__________________
If this post was helpful, click on the or donate me credits ^^
Reply With Quote
marshian is offlineReport Post
VIP User

Join Date: Jan 2008
Posts: 907
Credits: 478
bigjoe4 has a reputation beyond reputebigjoe4 has a reputation beyond repute
Quote  
05-27-2008, 02:31 PM
Re: Random numbers in PHP

lol that is cool I will definately put that on my site
__________________
Please visit my site: Circuit designer
Reply With Quote
bigjoe4 is offlineReport Post
x10 Sophmore

Join Date: Apr 2008
Posts: 205
Credits: 2,574
cursedpsp is on a distinguished road
Location: Wiltshire, England

Quote  
05-30-2008, 04:55 PM
Re: Random numbers in PHP

Quote:
Originally Posted by hamsn View Post
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
Reply With Quote
cursedpsp is offlineReport Post
x10 Elder

Join Date: Dec 2007
Posts: 529
Credits: 6,060
Alex Mac is just really nice
Location: Wellesbourne, England

Send a message via ICQ to Alex Mac Send a message via AIM to Alex Mac Send a message via MSN to Alex Mac Send a message via Yahoo to Alex Mac
Quote  
06-06-2008, 07:34 AM
Re: Random numbers in PHP

Like a confirmation code in a forum
__________________
Post on my forum: 1000 credits



Reply With Quote
Alex Mac is offlineReport Post
x10 Sophmore

Join Date: Apr 2008
Posts: 205
Credits: 2,574
cursedpsp is on a distinguished road
Location: Wiltshire, England

Quote  
11-09-2008, 02:33 PM
Re: Random numbers in PHP

Blimey, i found this atlast - i was searching for days for this (even using the search bar)
Reply With Quote
cursedpsp is offlineReport Post
Account Manager

sunils's Avatar

Join Date: Jan 2008
Posts: 2,033
Credits: 30,567
sunils has a reputation beyond repute
Location: Chennai ,India

Send a message via AIM to sunils Send a message via Yahoo to sunils
Quote  
11-13-2008, 07:43 AM
Re: Random numbers in PHP

A good post. will be helpfull to the newbie..
__________________
Sunil Sankar
-------------------------------------------------------------------------
Account Manager

Reply With Quote
sunils is offlineReport Post
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem compiling PHP zip extension onto a Mac that has MAMP Pro DarkAngelBGE Free Hosting 3 03-10-2008 01:22 PM
Unstand PHP? o0slowpaul0o Tutorials 8 01-07-2008 10:16 PM
Sigo con problemas con phpbb2 reciecho Soporte 7 10-20-2007 07:28 PM
"PHP Startup: Invalid Library" - Interesting error javaguy78 Free Hosting 5 03-27-2007 03:33 PM


All times are GMT -5. The time now is 03:08 PM. Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0 RC7
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

iPhone Reviews | Debt Help | Car Finance | Advertising | Libros recomendados