Jump to content

larger random numbers


Q695

Recommended Posts

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);

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)";}?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.