Jump to content

How to generate 15 random numbers between (1,10)?


darkslayer

Recommended Posts

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.

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

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?

 

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

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
}
}

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.