darkslayer Posted March 21, 2012 Share Posted March 21, 2012 Hi there. I need a function that can generate 15 random numbers between (1,10) and then converts those numbers to Roman numbers. Anyone can help me? Quote Link to comment https://forums.phpfreaks.com/topic/259449-how-to-generate-15-random-numbers-between-110/ Share on other sites More sharing options...
chrsm Posted March 21, 2012 Share Posted March 21, 2012 Have an array map of 1-10 to their Roman numeral equivalents. Create an empty array to store the generated numbers in. Use a for loop to go from 1 to 15 (or 0 to 14 if you are so inclined), each time storing the result from the random lookup in the (previously) empty array you created. For example: $numerals = array(1 => 'I', 2 => 'II', 3 => 'III', ...etc); $results = array(); for($i = 1; $i <= 15; $i++) { $rand = rand(1,10); $results[] = $numerals[$rand]; } And then functionize it. Quote Link to comment https://forums.phpfreaks.com/topic/259449-how-to-generate-15-random-numbers-between-110/#findComment-1329973 Share on other sites More sharing options...
Hall of Famer Posted March 21, 2012 Share Posted March 21, 2012 Umm I suppose this should suffice. You can also expand the class Rand to include other methods in future. class Rand{ static $Roman = array("I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X"); public static function getrand($num){ if($num < 1) die("Cannot generate less than one number..."); for($i =0; $i<$num; $i++){ $rand = mt_rand(0,9); $randnum[$i] = self::$Roman[$rand]; } return $randnum; } } // To output 15 Roman characters generated from the above method, simply do this $result = Rand::getrand(15); print_r($result); Quote Link to comment https://forums.phpfreaks.com/topic/259449-how-to-generate-15-random-numbers-between-110/#findComment-1329983 Share on other sites More sharing options...
darkslayer Posted March 21, 2012 Author Share Posted March 21, 2012 Have an array map of 1-10 to their Roman numeral equivalents. Create an empty array to store the generated numbers in. Use a for loop to go from 1 to 15 (or 0 to 14 if you are so inclined), each time storing the result from the random lookup in the (previously) empty array you created. For example: $numerals = array(1 => 'I', 2 => 'II', 3 => 'III', ...etc); $results = array(); for($i = 1; $i <= 15; $i++) { $rand = rand(1,10); $results[] = $numerals[$rand]; } And then functionize it. Can i use a While cicle to generate the 15 random numbers? And to convert the numbers into roman use a "SWITCH CASE" for each number? Quote Link to comment https://forums.phpfreaks.com/topic/259449-how-to-generate-15-random-numbers-between-110/#findComment-1329986 Share on other sites More sharing options...
smerny Posted March 21, 2012 Share Posted March 21, 2012 yes, but why would you want to do that? Quote Link to comment https://forums.phpfreaks.com/topic/259449-how-to-generate-15-random-numbers-between-110/#findComment-1329989 Share on other sites More sharing options...
darkslayer Posted March 21, 2012 Author Share Posted March 21, 2012 yes, but why would you want to do that? Is part of an exercise i have to do, and we can only use a While cicle and a "SWITCH CASE" to do it. Do you know how can i do it? Quote Link to comment https://forums.phpfreaks.com/topic/259449-how-to-generate-15-random-numbers-between-110/#findComment-1329993 Share on other sites More sharing options...
smerny Posted March 21, 2012 Share Posted March 21, 2012 should be able to figure it out easy enough based on whats posted here and a couple simple google searches... should learn from your hw instead of having other people do it for you. in fact... two simple google searches could find you all you need -php random number -php switch Quote Link to comment https://forums.phpfreaks.com/topic/259449-how-to-generate-15-random-numbers-between-110/#findComment-1329994 Share on other sites More sharing options...
smerny Posted March 21, 2012 Share Posted March 21, 2012 if you try and still cant figure it out, then ask Quote Link to comment https://forums.phpfreaks.com/topic/259449-how-to-generate-15-random-numbers-between-110/#findComment-1330005 Share on other sites More sharing options...
Psycho Posted March 21, 2012 Share Posted March 21, 2012 if you try and still cant figure it out, then ask And SHOW the work you have provided. Explain what you are trying to achieve and how the current results are different. Quote Link to comment https://forums.phpfreaks.com/topic/259449-how-to-generate-15-random-numbers-between-110/#findComment-1330007 Share on other sites More sharing options...
chrsm Posted March 21, 2012 Share Posted March 21, 2012 Have an array map of 1-10 to their Roman numeral equivalents. Create an empty array to store the generated numbers in. Use a for loop to go from 1 to 15 (or 0 to 14 if you are so inclined), each time storing the result from the random lookup in the (previously) empty array you created. For example: $numerals = array(1 => 'I', 2 => 'II', 3 => 'III', ...etc); $results = array(); for($i = 1; $i <= 15; $i++) { $rand = rand(1,10); $results[] = $numerals[$rand]; } And then functionize it. Can i use a While cicle to generate the 15 random numbers? And to convert the numbers into roman use a "SWITCH CASE" for each number? Yes. I won't provide the whole set of code, but here's some psuedocode for you: results = array(); i = 1; while(i <= 15) { switch(i) { case 1: results[] = 'I'; break; case 2: ...etc } } Quote Link to comment https://forums.phpfreaks.com/topic/259449-how-to-generate-15-random-numbers-between-110/#findComment-1330012 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.