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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.