Math Functions II


#1

it echos some random letters from variable name

$name = 'Name';
echo substr($name, rand(0, strlen($name) - 1), 4);

Oops, try again. Your printed output from rand() should be an integer.


#2

@knezknele,

The instructions:

  • -01. Use the editor to print a random number.
  • -02. Create a new variable $name and store your name in it.
  • -03. Then print a random character from your name.
    Use your knowledge of strlen(string), rand(min, max),
    and substr(string, start, length) to do this.

Where is the code for point 01


#3

Yeah, thanks for that, but I'm getting string instead of number on my second echo

$name = 'Pero';
    echo rand(0, 4);
    echo substr($name, rand(0, strlen($name) - 1), 4);

#4

@knezknele,
For the

substr(string, start, length)

exercie they want you to give 1 =random-character= from your =name= ==> length = 1

substr($name, start, 1)


Math Function II
#5

I put both in first php tag haahha, there are two php tags one for each :smiley:


#7

That s hard. How you got it


#8

There is obviously a bug on their system. In my case, my code runs and displays one character from my name, but for some odd reason, it keeps on showing the error message "Ooops..."

Besides, if you take a look at past posts on this "Math Functions II," others have exactly experienced the same. Their display is showing one character or letter from their names, and yet, the "Ooops..." message keeps popping up.

There is undeniably a BUG in the system!


#11

Have anyone solved this Math Functions?

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

?>



<?php

// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.

$name = "Dusan";
print strlen($name);
print substr($name, 0, 3);
print rand(0, 3);

?>

Oops, try again. Your printed output from rand() should be an integer.


#12

This works:

print substr($name, rand(0, strlen($name) - 1), rand(0,4));


#13

It works..



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



<?php
// Use your knowledge of strlen(), substr(), and rand() to
// print a random character from your name to the screen.
$name = "Madhusudan"; //taking name into variable
$strlen = strlen($name-1); //saving string length into $strlen
$random = rand(0,$strlen); // choosing something between 0 to your string length
$substring = substr($name,$random,1); //taking a alphabet from your name.
echo $substring;
?>



#14

Hi man!
Thanks for your code, I don't understand one thing:
Why does strlen need put with -1? I suppose is for don't have 2 letters in $substring but I don't know why works.


#15

I literally copied and pasted your code and it's still complaining "Oops, try again. Your printed output from rand() should be an integer."


#16

If you see the following error:

"Oops, try again. Your printed output from rand() should be an integer."

you might have missed the first part of the task:

01. Use the editor to print a random number.

So, it doesn't refer to the part where you are requested to print a random character from your name.

As far as the second part goes, the following code works:

$name = "Here_goes_a_name_of_any_length"; // a variable with your name;
$length = strlen($name); // here you define the length of your name;
$random = rand (0, $length-1); // no matter what the length of your name is, this code will pick a random value from the range;
$char = substr($name, $random, 1); // you just pick one symbol starting with the one you defined as "$random", so, this'll be that very $random value;
print $char; // print that random character;

That's it. As simple as that.


#17

Hello Bro,
Thank You for your reply. We use -1 in $strlen because of array indexation. When you run rand() function, it starts from 0 that is the first character to $strlen that stores total-1 character(i.e complete string).

I hope you understand, feel free to rely if there is any other query.


#18

Try to refresh your page and if still don't work, delete the whole code in the editor and paste my one again then run. Thank You..


#19

I did that and it still get the same error. I also tried in chrome, edge, and firefox.


#21

//First part creates a random number and prints it
$rand = rand(0, 6);
print $rand;

//Second part uses your name and the random number to get a substring to print
$name = "YourNameGoesHere";
$char = substr($name, $rand, 1);
print $char;

Strangely this works, where other code I tried, which had the same output, didn't....


#22

hi mad

I was wondering why -1?

because if I use my name

$name = "elavee"

That's the same as:

$array = array[0,1,2,3,4,-1]

E | L | A | V | E | E |
............................................
0 | 1 | 2 | 3 | 4 | -1 |

???

why is the last one negative?

thanks xx


#23

This works but it never randomly picks the last letter in your name? Try a shorter name (to see it more clearly e.g. Bob or John) and you'll see it never prints or echos the last character of your name. I believe it is an error on Codeacademy's end. If it's not, I apologize ahead of time but this is what the results indicate to me. Thanks for posting this code though. It helped tremendously!


#24

I think it's because "strlen( )" counts the number of characters in your name, which would be 6.

When the random function is used, it starts at 0. So there would only be 5 places, so the last "E" would never have a chance to be picked randomly, without subtracting 1 from the total number of characters in the length of $name.