Page 1 of 1
Creating random values
Posted: Sat Jul 19, 2003 11:48 pm
by php_wiz_kid
Hey everybody,
I have a question on creating random values. Yes, I searched through the database and everthing I found didn't help. I want something to generate a number or letter value. So it would end up looping let's say 24 times and I would get a value with a number/letter mix. I'm just not quite sure how I should go about this. Thanks.
Posted: Sun Jul 20, 2003 12:34 am
by trollll
You could try setting it up to pick random numbers that lie within the character codes for numbers and letters. Then for each randomization just take the character code and switch it over the the character itself.
Posted: Sun Jul 20, 2003 3:39 am
by award
http://www.evilwalrus.com/view.php?vs=category&cat=Misc
I was looking over this website the other day and they had some random number generating code, maybe worth a look at and trying that out.
Posted: Sun Jul 20, 2003 6:40 am
by pootergeist
or just
$rand_24_str = substr(base64_encode(MD5(rand(0,9999999))),0,24);
upper + lower case, alphanumeric string with 10 million permutations.
Posted: Sun Jul 20, 2003 8:17 am
by Gen-ik
Another way is to simply shuffle an array.
Code: Select all
<?php
$chrs = Array('1','2','3','4','5','6','7','8','9','0','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$string_length = 24;
$result = "";
for($i=0; $i<$string_length; $i++)
{
shuffle($chrs);
$result .= $chrs[0];
}
echo($result);
?>
The good thing about shuffle() is that you can pull out random numbers, letters, words, phrases, colours, anything you want really.
Posted: Sun Jul 20, 2003 8:24 am
by Drachlen
Code: Select all
<?php
$chars="0123456789";
mt_srand((double)microtime()*1000000);
for ($i=0;$i<24;$i++){
$rand .= $chars[mt_rand(0, strlen($chars)-1)];
}
echo $rand;
?>

Posted: Sun Jul 20, 2003 9:55 am
by award
I have used the shuffle() function and its good and simple (it has to be for me to use it!!)
Code: Select all
<?php
;
$rabbits = array("Stuff/rabbits1.jpg", "Stuff/rabbits2.jpg", "Stuff/rabbits3.jpg", "Stuff/rabbits4.jpg",
"Stuff/rabbits5.jpg", "Stuff/rabbits6.jpg");
shuffle($rabbits);
?>
<html>
<head>
<title>random bunny</title>
</head>
<body>
<center>
<table width = 100%>
<tr>
<?php
for ($i = 0; $i < 3; $i++)
{
echo "<td align = center><img src=" ";
echo $rabbitsї$i];
echo "" width = 100 height = 100></td>";
}
?>
</tr>
</table>
</center>
</body>
</html>
Not the best code but it does the job!