Jump to content

Conver string into a random number


dreamwest

Recommended Posts

How can i create a random 10 digit number for a string

 

Original string:

$string = "She walks down the street";

 

String converted to number

 

$string = "She walks down the street";

//Randomly generated string
$new_string = 1234613401198208671;

$file = str_replace($string,$new_string ,$string);

Link to comment
Share on other sites

That's like asking if an apple will do the same thing as an orange. They are both fruit, and you can live if you eat them both, but they are different things altogether.

 

Those will both produces a number, same as apples and oranges will both produce flavors. But the first one will produce a random number, and the second one will produce the current unix timestamp.

Link to comment
Share on other sites

$num = rand(1000000000,9999999999);
echo $num;

 

nvm I don't think that will work, I tested it, and I never got anything that started with a 2+

 

but this will work:

<?php
function gen_rand($max = 10){
     $num = '';
     for($i=0;$i<$max;$i++){
          $num .= rand(0,9);
     }
     return $num;
}

echo gen_rand();
?>

Link to comment
Share on other sites

No.

 

mt_rand() is a (psuedo) random number.  time() is the number of seconds since 00:00 Jan 1, 1970.

 

But still a random number. Sweet!

 

That's like asking if an apple will do the same thing as an orange. They are both fruit, and you can live if you eat them both, but they are different things altogether.

 

It was just an example...like an orange ad an apple - both are round.

 

Simple logic really

Link to comment
Share on other sites

No.

 

mt_rand() is a (psuedo) random number.  time() is the number of seconds since 00:00 Jan 1, 1970.

 

But still a random number. Sweet!

 

 

If you are wanting a 10 digit unique number and it doesn't matter if someone figures out the pattern etc.. then yes, time() will work for you.  But your OP was talking about "converting" a string to a number, which to me sounds like you are wanting to encrypt it.  If that is the case, simply picking a random number to take the place of the string does not encrypt it, all by itself.  You would have to keep a table of string => number  to know what the number stands for.

 

Perhaps if you posted what your goal here is, you might get an optimal solution.

Link to comment
Share on other sites

Perhaps if you posted what your goal here is, you might get an optimal solution.

 

I want to add time() and mt_rand() together so when i rename a file to a number and insert the name into the database there wont be duplicate names, and it makes it harder for ppl to guess the file name and try and use a download manager or script to download the entire directory of files.

 

Currently im embedding content:

 

file=http://site.com/files/name_of_file.mpg

 

But i want to ultimately us this system:

 

file=http://site.com?video_key=random_key_here

 

Maybe a htaccess config might do...

Link to comment
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.