Math Functions II


#1

hello everyone i need somebody to help.
what is solution i am stuck here.. :cry:




<?php
// Use rand() to print a random number to the screen
print rand(1,20);
?>

<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$name ="Andi Susanto";
$i = strlen($name);
echo $i;
?>
</p>



#2

You have to print a random character of your name. Not the whole name.

Here my example:

     <p>
     <?php
     // Use your knowledge of strlen(), substr(), and rand() to
     // print a random character from your name to the screen.
     $name = "Dominik";
     $strlen = strlen($name);
     $rand2 = rand(0, $strlen);
     $rand3 = round($rand2);
     $substr = substr($name, 0, $rand3);
     print $substr;
     </p>

With the random function i created a number between 1 and and the length of my name ($strlen). This number I embed into the the round funtion and this into the substr function. Then i printed out the substr.


#3

it isn't good, because third parameter sets length of substr().

good code:

 $name = "Dominik";
 $strlen = strlen($name); // 7
 $rand2 = rand(0, $strlen - 1); // from 0 to 6
 $substr = substr($name, $rand2, 1);
 print $substr;

#4

OK, Tank You All... Very help..


#5

You can achieve the same with less code. This is what I used and it worked:

$name= "Ngonidzashe";
$rand= rand(0,strlen($name));
echo substr($name,$rand,1);


#6

Hmm, so I didn't subtract 1 from my string length, but it still works... Will this be a problem in more complex code??

Here's what I came up with (no help!! Yippee):

$myName = "Sabrina";
$myNameLen = strlen($myName);
$myNameRand = rand(0, $myNameLen);
print $myName[$myNameRand];

#7

Your code will have an error one out of eight times, because the rand function returns a random integer between the min and max argument inclusive.

That means rand(0, 7) can return 0 and 7.

If it returns 0, 1 , 2, 3 , 4 , 5, or 6 there is no problem, because "Sabrina" has letters at those indexes. There is no letter at index 7 because the first letter is at index 0, the second letter at index 1 and so on, up to the seventh letter at index 6. That's why you have to subtract one from the length of your name.


#8

...even a lesser code!
Check this out:

$name="MyCoolName";
print substr($name, rand(0, (strlen($name)-1)), 1);


#9

this is how i managed to do it. though i really like your code @jmsofts1 !

$name="rikdol";
$position=strlen($name);
$random=($position)-rand(1,($position)-1);
print substr($name,$random,1)


#10

Yes, you are absolutely correct .Thanks


#11

I spent absolutely ages on this but finally got it working using the following - I've commented beside my code to help explain.

$myName = "Alexander The Great";
$length = strlen($myName); //<--- Length of variable $myName.
//print $length; //<--- This was just to check my variable output
$randomChar = rand(0, $length-1); //<--- Generate a random number between 0 and -1 of the length of $myName. -1 required due to zero indexing.
//print $random; //<--- This was just to check my variable output
$sub = substr($myName, $randomChar, 1); //<--- get substring from $myName, using $randomChar as the start of substring, returning 1 character.
print $sub;


#12



<?php
// Use rand() to print a random number to the screen
echo rand();

?>
</p>
<p>
<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$name = "alif";
echo strlen($name);
$random = rand(0, 3);
echo $name[$random];


?>
</p>


it can also!


#13

even the answer correct, it still popup "Oops, try again. The random output you printed from your name should be just one character.", It was a bug, pls fix it, thx.


#17