Creating random values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
php_wiz_kid
Forum Contributor
Posts: 181
Joined: Tue Jun 24, 2003 7:33 pm

Creating random values

Post 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.
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post 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.
User avatar
award
Forum Newbie
Posts: 13
Joined: Tue Jul 15, 2003 10:45 am
Location: Wakefield, UK
Contact:

Post 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.
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

Post 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.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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.
Last edited by Gen-ik on Sun Jul 20, 2003 8:30 am, edited 1 time in total.
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Post 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;
?>
:D
User avatar
award
Forum Newbie
Posts: 13
Joined: Tue Jul 15, 2003 10:45 am
Location: Wakefield, UK
Contact:

Post 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++)
   &#123;
    echo "<td align = center><img src=" "; 
    echo $rabbits&#1111;$i]; 
    echo "" width = 100 height = 100></td>";
   &#125;
?>
</tr>
</table>
</center>
</body>
</html>
Not the best code but it does the job!
Post Reply