# larger random numbers

Go to solution Solved by bleured27,

## Recommended Posts

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 by Q695
##### Share on other sites

"Range" how? You're hashing the only thing in there that's random so you're not even getting a number out of it.

##### Share on other sites

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.

##### Share on other sites

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);
##### Share on other sites

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.

##### Share on other sites

Does anyone know what charicters would be outerrored by PHP/MySQL

I so far have 'abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';

Edited by Q695
##### Share on other sites

How large a range of numbers are you looking for, are 2147483648 numbers not enough?

##### Share on other sites

I'm trying to reduce the chance of repeating more than 62^64.

##### Share on other sites

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.

##### Share on other sites

• Solution

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 by bleured27
##### Share on other sites

I'm thinking kind of like how huge photo sites randomly name their photos.

##### Share on other sites

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)

##### Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.