Lunarpages Web Hosting
 
Home Register FAQ Members List Calendar Search Today's Posts Mark Forums Read Web Directory

Go Back   Webmaster Forum > Designing and Developing Websites > Programming > PHP / MySQL

PHP / MySQL Discussions and help on PHP and MySQL.

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 03-24-2007, 05:41 PM
J0ker J0ker is offline
WMG Resident Alien
 
Join Date: Mar 2007
Posts: 109
iTrader: (0)
J0ker is on a distinguished road
Default Simple Random Number

This is a very simple but useful technique. There are a lot of instances where you will need random numbers, and it might be hard to see where you would, but I have use them lots of times for scripts and found this command extremely useful.

PHP Code:
rand(1,100
This would create a random number between 1 and 100. In most cases you will be puting it to a variable so this would be your code:

PHP Code:
$variable rand(1,250
This sets a random number between 1 and 250 to the variable.
Reply With Quote
Sponsored Links
Register and sign in to hide this ad block

  #2 (permalink)  
Old 03-26-2007, 03:58 AM
creek23 creek23 is offline
WMG Deputy Sheriff
 
Join Date: Mar 2007
Posts: 290
iTrader: (0)
creek23 is on a distinguished road
Default

perhaps you should also discuss the maximum value for random number
Reply With Quote
  #3 (permalink)  
Old 03-26-2007, 05:39 AM
technoguy technoguy is offline
WMG Citizen
 
Join Date: Mar 2007
Posts: 436
iTrader: (0)
technoguy is on a distinguished road
Default

and rand function without argument will give any random value of integer range.
Reply With Quote
  #4 (permalink)  
Old 03-26-2007, 02:47 PM
blokey blokey is offline
WMG Resident Alien
 
Join Date: Mar 2007
Posts: 93
iTrader: (0)
blokey is on a distinguished road
Default

You'd be better off using mt_rand() which produces better random numbers using a Mersenne Twister, and it's also faster than rand().
Reply With Quote
  #5 (permalink)  
Old 03-30-2007, 08:35 AM
kaisellgren kaisellgren is offline
WMG Newcomer
 
Join Date: Mar 2007
Posts: 38
iTrader: (0)
kaisellgren is on a distinguished road
Default

I agree with blokey.

And if you want random hashes, this should be sufficent:

Quote:
$hash = md5(uniqid(rand(),true));
That gives you 32 characters long string.
Reply With Quote
  #6 (permalink)  
Old 03-31-2007, 07:20 AM
creek23 creek23 is offline
WMG Deputy Sheriff
 
Join Date: Mar 2007
Posts: 290
iTrader: (0)
creek23 is on a distinguished road
Default

why would some one need a random hash? for validation?
Reply With Quote
  #7 (permalink)  
Old 04-01-2007, 03:46 AM
technoguy technoguy is offline
WMG Citizen
 
Join Date: Mar 2007
Posts: 436
iTrader: (0)
technoguy is on a distinguished road
Default

I am using random numbers for my online examination project, it is possible to avoid question alrady displayed not to displayed again ?
Reply With Quote
  #8 (permalink)  
Old 04-02-2007, 01:01 AM
creek23 creek23 is offline
WMG Deputy Sheriff
 
Join Date: Mar 2007
Posts: 290
iTrader: (0)
creek23 is on a distinguished road
Default

isn't it that you made a function to randomly get a row of items from a dbase table? modify and use it.
Reply With Quote
  #9 (permalink)  
Old 01-24-2008, 08:46 AM
zhar zhar is offline
WMG Newcomer
 
Join Date: Jan 2008
Posts: 25
iTrader: (0)
zhar is on a distinguished road
Default

Where all can we use rand() , i use it only for captcha's . Can it be used for any better things.
Reply With Quote
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

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

All times are GMT. The time now is 05:55 AM.



Freelance Web Designers
Work At Home Forum
Ad Marketplace
Online Deals and Bargains
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.0.0