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
https://forums.phpfreaks.com/topic/117098-problem-with-rand/
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
https://forums.phpfreaks.com/topic/117098-problem-with-rand/#findComment-602313
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
https://forums.phpfreaks.com/topic/117098-problem-with-rand/#findComment-602349
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
https://forums.phpfreaks.com/topic/117098-problem-with-rand/#findComment-602356
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
https://forums.phpfreaks.com/topic/117098-problem-with-rand/#findComment-602361
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
https://forums.phpfreaks.com/topic/117098-problem-with-rand/#findComment-602366
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
https://forums.phpfreaks.com/topic/117098-problem-with-rand/#findComment-602389
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
https://forums.phpfreaks.com/topic/117098-problem-with-rand/#findComment-602403
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.