dreamwest Posted February 16, 2009 Share Posted February 16, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/145359-conver-string-into-a-random-number/ Share on other sites More sharing options...
gevans Posted February 16, 2009 Share Posted February 16, 2009 what do you mean 'for a string' why not just create a random ten digit integer? Quote Link to comment https://forums.phpfreaks.com/topic/145359-conver-string-into-a-random-number/#findComment-763071 Share on other sites More sharing options...
dreamwest Posted February 16, 2009 Author Share Posted February 16, 2009 Ok thatll work....But how to create a random number?? Quote Link to comment https://forums.phpfreaks.com/topic/145359-conver-string-into-a-random-number/#findComment-763073 Share on other sites More sharing options...
gevans Posted February 16, 2009 Share Posted February 16, 2009 <?php function randInt($len=10) { $randInt = NULL; $randIntArray = range(0,9); for($i=0;$i<$len;$i++) { $randInt .= $randIntArray[array_rand($randIntArray)]; } return $randInt; } echo randInt(); Quote Link to comment https://forums.phpfreaks.com/topic/145359-conver-string-into-a-random-number/#findComment-763077 Share on other sites More sharing options...
dreamwest Posted February 16, 2009 Author Share Posted February 16, 2009 Ive seen this somewhere mt_rand( ) Will this do the same thing as say time() Quote Link to comment https://forums.phpfreaks.com/topic/145359-conver-string-into-a-random-number/#findComment-763130 Share on other sites More sharing options...
corbin Posted February 16, 2009 Share Posted February 16, 2009 No. mt_rand() is a (psuedo) random number. time() is the number of seconds since 00:00 Jan 1, 1970. Quote Link to comment https://forums.phpfreaks.com/topic/145359-conver-string-into-a-random-number/#findComment-763157 Share on other sites More sharing options...
haku Posted February 16, 2009 Share Posted February 16, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/145359-conver-string-into-a-random-number/#findComment-763159 Share on other sites More sharing options...
The Little Guy Posted February 16, 2009 Share Posted February 16, 2009 $num = rand(1000000000,9999999999); echo $num; Quote Link to comment https://forums.phpfreaks.com/topic/145359-conver-string-into-a-random-number/#findComment-763178 Share on other sites More sharing options...
The Little Guy Posted February 16, 2009 Share Posted February 16, 2009 $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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/145359-conver-string-into-a-random-number/#findComment-763185 Share on other sites More sharing options...
dreamwest Posted February 16, 2009 Author Share Posted February 16, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/145359-conver-string-into-a-random-number/#findComment-763224 Share on other sites More sharing options...
haku Posted February 16, 2009 Share Posted February 16, 2009 time() isn't a random number at all. It is 100% predictable. Quote Link to comment https://forums.phpfreaks.com/topic/145359-conver-string-into-a-random-number/#findComment-763324 Share on other sites More sharing options...
.josh Posted February 16, 2009 Share Posted February 16, 2009 $num = rand(1000000000,9999999999); echo $num; nevermind I don't think that will work, I tested it, and I never got anything that started with a 2+ You are right; it won't work. rand generates a random 32 bit integer. Quote Link to comment https://forums.phpfreaks.com/topic/145359-conver-string-into-a-random-number/#findComment-763374 Share on other sites More sharing options...
.josh Posted February 16, 2009 Share Posted February 16, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/145359-conver-string-into-a-random-number/#findComment-763377 Share on other sites More sharing options...
dreamwest Posted February 16, 2009 Author Share Posted February 16, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/145359-conver-string-into-a-random-number/#findComment-763757 Share on other sites More sharing options...
gevans Posted February 17, 2009 Share Posted February 17, 2009 Unless you expect to see more than one file upload at any one second just use time(). Quote Link to comment https://forums.phpfreaks.com/topic/145359-conver-string-into-a-random-number/#findComment-763796 Share on other sites More sharing options...
haku Posted February 17, 2009 Share Posted February 17, 2009 Or if you are worried about more than one upload a second put an auto-incremented ID field in the table, and append the ID to the filename. Quote Link to comment https://forums.phpfreaks.com/topic/145359-conver-string-into-a-random-number/#findComment-763920 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.