Problems with random numbers...

This is a discussion on "Problems with random numbers..." within the PHP Forum section. This forum, and the thread "Problems with random numbers... are both part of the Program Your Website category.



Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Dec 24th, 2006, 00:03
Reputable Member
Join Date: Sep 2005
Location: Canada, BC
Age: 24
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Problems with random numbers...

What should this code generate? My guess, 50 random numbers all between two fairly large numbers.
PHP: Select all

<ol><?php

for($x=0;$x<100;$x++)
{
echo 
'<li>'.mt_rand(48711122,2435556100).'</li>';
}
?></ol>
Actual Result? A little bit stranger...
HTML: Select all
<ol><li>-1399182791</li><li>-41036583</li><li>-1252920765</li><li>-526451647</li><li>-332961694</li><li>-1833380612</li><li>-1159768753</li><li>-1813882781</li><li>-1550466255</li><li>-68989001</li><li>-1659861049</li><li>-441480022</li><li>-183657088</li><li>-1277516243</li><li>-667125714</li>...
Okay... so... what now? I need a random generator to be able to handle numbers this large, I would simply times them all by -1, however some times it actually throws out a positive number, now I could come up with a way around that, but i'm sure there is a "proper" way to do this... right?
Reply With Quote

  #2 (permalink)  
Old Dec 24th, 2006, 01:36
Ryan Fait's Avatar
SuperMember

SuperMember
Join Date: May 2006
Location: Las Vegas
Posts: 3,786
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Problems with random numbers...

It's because the second mt_rand variable is too large by one number. You can do something like this:

PHP: Select all

<?php
$rand 
mt_rand(487,243).mt_rand(11122,5556100);
?>
Reply With Quote
  #3 (permalink)  
Old Dec 24th, 2006, 08:38
Reputable Member
Join Date: Sep 2005
Location: Canada, BC
Age: 24
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Problems with random numbers...

Yeah I found a better way of doing it
$x * rand(1,50) instead of rand($x,$x*50)
But its kinda strange that the max number isn't mentioned anywhere in the documentation for php
Reply With Quote
  #4 (permalink)  
Old Jan 21st, 2007, 23:02
Junior Member
Join Date: Jan 2006
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Problems with random numbers...

I've always found I get much better results with mt_rand() than with rand(), e.g. with things like sudoku generators.
Reply With Quote
Reply

Tags
mt_rand, php, rand

Thread Tools

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
[SOLVED] My page numbers are falling off of my website!! 62vye Web Page Design 20 Mar 24th, 2008 18:28
Incrementing numbers RubyRue ASP.NET Forum 3 Mar 12th, 2008 03:25
Specific searchable directory, think phonebook with more than numbers AmirKhan Website Planning 0 May 21st, 2007 19:38
webdesigners with phone numbers contact me jacob Job Opportunities 5 Dec 9th, 2005 09:40
Selecting Numbers WillisTi Flash & Multimedia Forum 1 Nov 16th, 2005 16:56


All times are GMT. The time now is 20:52.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43