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.
Creating random values
Moderator: General Moderators
-
php_wiz_kid
- Forum Contributor
- Posts: 181
- Joined: Tue Jun 24, 2003 7:33 pm
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.
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.
Last edited by award on Sun Jul 20, 2003 7:21 am, edited 1 time in total.
-
pootergeist
- Forum Contributor
- Posts: 273
- Joined: Thu Feb 27, 2003 7:22 am
- Location: UK
Another way is to simply shuffle an array.
The good thing about shuffle() is that you can pull out random numbers, letters, words, phrases, colours, anything you want really.
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);
?>
Last edited by Gen-ik on Sun Jul 20, 2003 8:30 am, edited 1 time in total.
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;
?>I have used the shuffle() function and its good and simple (it has to be for me to use it!!)
Not the best code but it does the job!
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>