php rand() not random... lol

  • Danny1337
  • Student
  • Student
  • Danny1337
  • Posts: 70
  • Loc: Norway

Post 3+ Months Ago

I'm using the rand() function to generate random numbers between 20 and 235, to insert into a table using mysql. But it seems like this function loves the number 127 a lot..

Code: [ Select ]
$color_red = rand(20,235);
$color_green = rand(20,235);
$color_blue = rand(20,235);
  1. $color_red = rand(20,235);
  2. $color_green = rand(20,235);
  3. $color_blue = rand(20,235);


Out of 30 queries, like half of the results are 127.. (attached screenshot below)

I tried to add this following code at the top, but doesn't seems to help.
Code: [ Select ]
srand ((double) microtime( )*1000000);


Isn't rand() function random?
Attachments:
rnd.jpg
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Does the mt_rand function provide you with better results when you try it ?

Have you compared performance/randomness of generating a single random number and using bit-shifting to get your RGB ?
  • Danny1337
  • Student
  • Student
  • Danny1337
  • Posts: 70
  • Loc: Norway

Post 3+ Months Ago

mt_rand didn't make any difference. I didn't understand your second part,lol.

I tried to run this code just to see the values

Code: [ Select ]
<?php
$color_red = rand(20,235);
$color_green = rand(20,235);
$color_blue = rand(20,235);

echo $color_red . " " . $color_green . " " . $color_blue;
?>
  1. <?php
  2. $color_red = rand(20,235);
  3. $color_green = rand(20,235);
  4. $color_blue = rand(20,235);
  5. echo $color_red . " " . $color_green . " " . $color_blue;
  6. ?>


And it never landed on 127. Weird. It seems to be totally random. but when they are inserted to the database, they definitely aren't random


edit*
I think i figured out why.. I had the table columns set to TINYINT. it seems to work better with SMALLINT. Not sure why it matters, since TINYINT should work up to 255. oh well
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

The part about inserting them into MySQL slipped my mind before, it should have been obvious with the 127 in hindsight.

Take a look at the columns data type you're using to store your random numbers in the data base. It's a TINYINT isn't it ? I bet it's a SIGNED TINYINT too isn't it ?

If it is, switching it to an UNSIGNED TINYINT should have you going.

I think what is happening is that every value you insert that's over 127 is getting truncated to 127 because of the TINYINT being SIGNED and thus limited to -127 to 127 instead of zero to 255.

// edit -- Didn't notice your edit until now. :)
  • Danny1337
  • Student
  • Student
  • Danny1337
  • Posts: 70
  • Loc: Norway

Post 3+ Months Ago

Heh, yeah.
<-- feels stupid.

Thank you =)

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 64 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.