Q695 Posted April 24, 2013 Share Posted April 24, 2013 (edited) Is there a way to create a larger range of random numbers than this code will create, if so how? $path= "$path" . md5(mt_rand(0, mt_getrandmax())). '.jpg'; Edited April 24, 2013 by Q695 Quote Link to comment Share on other sites More sharing options...
requinix Posted April 24, 2013 Share Posted April 24, 2013 "Range" how? You're hashing the only thing in there that's random so you're not even getting a number out of it. Quote Link to comment Share on other sites More sharing options...
Q695 Posted April 24, 2013 Author Share Posted April 24, 2013 The name is appearing random, and inside the md5 you're generating a random name. It would be nice if the mt_rand could do letters, and numbers. Quote Link to comment Share on other sites More sharing options...
Joshua F Posted April 24, 2013 Share Posted April 24, 2013 I found this script on a different site and it works fine for generating random letters/numbers. function get_random_string($valid_chars, $length) { // start with an empty random string $random_string = ""; // count the number of chars in the valid chars string so we know how many choices we have $num_valid_chars = strlen($valid_chars); // repeat the steps until we've created a string of the right length for ($i = 0; $i < $length; $i++) { // pick a random number from 1 up to the number of valid chars $random_pick = mt_rand(1, $num_valid_chars); // take the random character out of the string of valid chars // subtract 1 from $random_pick because strings are indexed starting at 0, and we started picking at 1 $random_char = $valid_chars[$random_pick-1]; // add the randomly-chosen char onto the end of our string so far $random_string .= $random_char; } // return our finished random string return $random_string; } Example $original_string = 'abcdefghi'; $random_string = get_random_string($original_string, 6); Quote Link to comment Share on other sites More sharing options...
requinix Posted April 24, 2013 Share Posted April 24, 2013 Hashes are not random. If you want an actual random string of letters and numbers then do like Joshua F showed and actually make a random string. Quote Link to comment Share on other sites More sharing options...
Q695 Posted April 24, 2013 Author Share Posted April 24, 2013 (edited) Does anyone know what charicters would be outerrored by PHP/MySQL I so far have 'abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ'; Edited April 24, 2013 by Q695 Quote Link to comment Share on other sites More sharing options...
salathe Posted April 24, 2013 Share Posted April 24, 2013 How large a range of numbers are you looking for, are 2147483648 numbers not enough? Quote Link to comment Share on other sites More sharing options...
Q695 Posted April 24, 2013 Author Share Posted April 24, 2013 I'm trying to reduce the chance of repeating more than 62^64. Quote Link to comment Share on other sites More sharing options...
salathe Posted April 24, 2013 Share Posted April 24, 2013 Why? It looks like you're only trying to make a unique file name; you seem to be over-thinking this problem somewhat. Anyway, if you don't want repetition then something as basic as 1, 2, 3 ... does that. Quote Link to comment Share on other sites More sharing options...
Solution bleured27 Posted April 24, 2013 Solution Share Posted April 24, 2013 (edited) you could make random numbers whith pi and time to get pi as exactly as possible use this calculation . <table><tr><?php $c='3';$d=1;$md=1000000; $s=3; $pi =10000000000000; $pi2=10000000000000; while($d<=$md){ if($s==4) { $pi=$pi+($pi2/$c); $s=3; $pi3=$pi*4; $result = mb_substr($pi3, 0, 1); $result2 = mb_substr($pi3, 1, 300); echo"<td>line".$d."</td><td>:</td><td>";echo $result.".".$result2."</td></tr><tr>"; } else{ $pi=$pi-($pi2/$c); $s=4; $pi3=$pi*4; $result = mb_substr($pi3, 0, 1); $result2 = mb_substr($pi3, 1, 300); echo"<td>line".$d."</td><td>:</td><td>";echo $result.".".$result2."</td></tr><tr>"; } $c=$c+2; $d=$d+1; } $pi=$pi*4; echo $pi; ?> </table> (php might not be the best script to do this) and use a calculation whith pi to take a digit (i always use this way to get random numbers.) <?php $result = mb_substr($pi, 1, 3); //this takes the 2,3,4 character of the variable $pi $result = mb_substr($pi, 1, 2); //this takes the 2,3 character of the variable $pi //so mb_substr($pi[difines the variable's characters you take] //mb_substr($pi, 1[difines the characters you skip,change it to a calculation of time for random] //mb_substr($pi, 1, 2[this difines how long your new variable is]); //so $number ="390534"; $result = mb_substr($number, 1, 4);//this makes 9053 //so result="9053"; ?> to find pi better in php(les likly to crash your server/browser) (origanily mad for testing php's calculating skills this didn't work 100% properly becose if you put in a to high number(in the code not in the form) you get the hexidemential (or something like that) numbers) <form action="" method="post"> <input type="text" name="md"> <input type="submit" value="calculate"> </form> <?php if(isset($_POST['md'])){ ?> <table><tr><?php $c='3';$d=1;$md=$_POST['md']; $s=3; $pi =10000000000000; $pi2=10000000000000; while($d<=$md){ if($s==4) { $pi=$pi+($pi2/$c); $s=3; $pi3=$pi*4; $result = mb_substr($pi3, 0, 1); $result2 = mb_substr($pi3, 1, 300); echo"<td>line".$d."</td><td>:</td><td>";echo $result.".".$result2."</td></tr><tr>"; } else{ $pi=$pi-($pi2/$c); $s=4; $pi3=$pi*4; $result = mb_substr($pi3, 0, 1); $result2 = mb_substr($pi3, 1, 300); echo"<td>line".$d."</td><td>:</td><td>";echo $result.".".$result2."</td></tr><tr>"; } $c=$c+2; $d=$d+1; } $pi=$pi*4; $result = mb_substr($pi3, 0, 1); $result2 = mb_substr($pi3, 1, 300); echo"best result =".$result.".".$result2; ?> </table> <?php }else{echo"see how many pi multiplayers your pc can handle<br /> pi=4*(1-1/3[multiplayer1]+1/5[multiplayer2] etcetra)";}?> Edited April 24, 2013 by bleured27 Quote Link to comment Share on other sites More sharing options...
Q695 Posted April 24, 2013 Author Share Posted April 24, 2013 I'm thinking kind of like how huge photo sites randomly name their photos. Quote Link to comment Share on other sites More sharing options...
bleured27 Posted April 25, 2013 Share Posted April 25, 2013 oh well they do that whith an variation of id becose id never is the save you can't get the same picture. (don't you know id?=>learn mysql or an other databese that connects whith php) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.