Jump to content

Problem with rand()


sharpnova

Recommended Posts

I'm having a very strange and specific problem with rand().

 

for example:

 

for ($i=0; $i<10; $i++) echo rand()%3;

 

works just fine. shows 10 integers from 0 to 2 and they all change randomly everytime i refresh the page

 

in fact it works for most #'s

 

but for 2 it doesn't

 

for ($i=0; $i<10; $i++) echo rand()%2;

 

I ALWAYS get 0101010101.

 

Always. No randomness no repitition. Nothing.

 

Occasionally (when I change the value to a working # then change it back) I will get the following behavior:

 

refreshing the page will (randomly) give either 0101010101 or 1010101010

Which of the two is given will be random but it never deviates from those two results.

 

Also it's unpredictable when it will even work this much. Sometimes it will just refresh and give 0101010101 forever.

 

I know it isn't a browser issue because for other modulus values, it works fine.

 

update:

 

now it's messing up with 4 as well. 4 alternates (NOT randomly) between 2301230123 and 0123012301.

Link to comment
Share on other sites

i am not sure how php generates random numbers but on my linux box i get rand 1011010110 and nothing like you say.

what os are you running maybe it has something to do with how it generates random numbers

 

Scott.

 

Windows XP media center edition. (yes not the most ideal for php hosting)

 

It works fine if I mod rand() with anything other than 2 or 4. But for 2 and 4 it just doesn't seem to work. I can't find anything on the web and no one I've talked to has any clue.

 

But you can see for yourself:  http://www.sharpnova.com/random.php

 

The contents of the php file that link points to are:

 

<?php

for ($i=0; $i<10; $i++) print rand()%2;

 

?>

 

It may also help mentioning that I'm currently using the PHP that comes bundled with whatever is the latest version of XAMPP

Link to comment
Share on other sites

that's really random try this

for ($i=0; $i<10; $i++) print rand()."<br>";

just to see what random numbers it is making.

 

Scott.

 

I did what you said and that is uploaded and you can see it at http://www.sharpnova.com/random.php

 

as you can see it's generating different numbers everytime but always in the pattern of even, odd, even, odd, even, odd, even, odd, even, odd

 

This is a very strange problem.

Link to comment
Share on other sites

Windows XP media center edition. (yes not the most ideal for php hosting)

 

It may also help mentioning that I'm currently using the PHP that comes bundled with whatever is the latest version of XAMPP

 

Nothing wrong with XP media center edition, I use it myself  :D

 

I don't know, but maybe possibly a setting in php.ini may be the problem. But I doubt it.

Link to comment
Share on other sites

I've tried with and without seeding and with and without ranges inside rand itself.

 

I can see the same problem both with and without seeding when no range is given.

 

try: print rand(0,9) %2;

(my results:
111100010
110101110
111110101
100111010
100110011
001101100
000100101
001101011
001111010
000110101)

Link to comment
Share on other sites

Wow....  I never knew that rand() will [seemingly] always return an alternating sequence of even/odd numbers.  I guess it makes sense, since there's a 1/2 chance (well.... for rand()) of a number being even or odd, but I wouldn't expect them to be in order.

 

 

How about mt_rand()?

Link to comment
Share on other sites

Wow....  I never knew that rand() will [seemingly] always return an alternating sequence of even/odd numbers.  I guess it makes sense, since there's a 1/2 chance (well.... for rand()) of a number being even or odd, but I wouldn't expect them to be in order.

 

 

How about mt_rand()?

my results and Gamic results show it doesn't return alternating odd and even numbers

 

Scott.

Link to comment
Share on other sites

Wow....  I never knew that rand() will [seemingly] always, under certain conditions, return an alternating sequence of even/odd numbers.  I guess it makes sense, since there's a 1/2 chance (well.... for rand()) of a number being even or odd, but I wouldn't expect them to be in order.

 

 

How about mt_rand()?

 

 

Better?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.