There's more then one way to make a random number

• There's this fellow I've been trying to help on PHP forum. Pretty
intresting case all in all, but I found this beauty from his code. His
idea is to create a random number for some kind of user check. This is
what he has come up with:

\$length    = 8;
\$key_chars = '0123456789';
\$rand_max  = strlen(\$key_chars) - 1;

for (\$i = 0; \$i < \$length; \$i++)
{
\$rand_pos  = rand(0, \$rand_max);
\$rand_key[] = \$key_chars{\$rand_pos};
}

\$randomnumber = implode('', \$rand_key);

• @LazyJones said:

There's this fellow I've been trying to help on PHP forum. Pretty intresting case all in all, but I found this beauty from his code. His idea is to create a random number for some kind of user check. This is what he has come up with:

\$length    = 8;
\$key_chars = '0123456789';
\$rand_max  = strlen(\$key_chars) - 1;

for (\$i = 0; \$i < \$length; \$i++)
{
\$rand_pos  = rand(0, \$rand_max);
\$rand_key[] = \$key_chars{\$rand_pos};
}

\$randomnumber = implode('', \$rand_key);

The reason it's done this way is, if not obvious, not far-fetched. It can be easily expanded to include letters and special characters:

<FONT face="Courier New">\$key_chars = '0123456789aAbBcC*%.:,';</FONT>

The current key_chars string, and the name of the resulting variable (\$randomnumber) is slightly wtf-worthy, though.

• @welcor said:

@LazyJones said:

There's this fellow
I've been trying to help on PHP forum. Pretty intresting case all in
all, but I found this beauty from his code. His idea is to create a
random number for some kind of user check. This is what he has come up
with:

\$length    = 8;
\$key_chars = '0123456789';
\$rand_max  = strlen(\$key_chars) - 1;

for (\$i = 0; \$i < \$length; \$i++)
{
\$rand_pos  = rand(0, \$rand_max);
\$rand_key[] = \$key_chars{\$rand_pos};
}

\$randomnumber = implode('', \$rand_key);

The reason it's done this way is, if not obvious, not far-fetched. It can be easily expanded to include letters and special characters:

<font face="Courier New">\$key_chars = '0123456789aAbBcC*%.:,';</font>

The current key_chars string, and the name of the resulting variable (\$randomnumber) is slightly wtf-worthy, though.

\$randomnumber doesn't even hold a number, it's a string! So either he was going to do what welcor said and named the variable wrong, or he used a bad method. There's no getting out of this one

• Still, intention and execution are sound enough.

The misnaming of variables is never really that much of a WTF.

• @Shen said:

\$randomnumber doesn't even hold a number, it's a string! So either he
was going to do what welcor said and named the variable wrong, or he
used a bad method. There's no getting out of this one

Unless he actually wants to hold it as a string (e.g. he is going to print it out in an image and make the user type it in--he may want the string to be a certain length, so if there is a zero in front, that is still part of the string the user has to type in...)

Just a thought.

• @Shen said:

\$randomnumber doesn't even hold a number, it's a string! So either he
was going to do what welcor said and named the variable wrong, or he
used a bad method. There's no getting out of this one

It's php. It's a number. And a string

• @xiphias said:

It's php. It's a number. And a string

Right.  PHP treats "32" and 32 identically.  Ahh, the joys of an interpreted language.
-FM

• PHP most definitely has types. See the === operator, and its type comparison tables.

And why risk it, it might break in the future

• Ok, so what would be your solution?

The result must be a string of fixed length (unknown but given) containing only digits.

Let's see who will come with the shorter/faster/cleaner code.

• @trollable said:

Ok, so what would be your solution?

The result must be a string of fixed length (unknown but given) containing only digits.

Let's see who will come with the shorter/faster/cleaner code.

The WTF here is that the person did not use PHP's rand() function. My solution is the following:

\$length = 8; // Or another desired length.
\$random = '';

for( \$i = 0; \$i < \$length; \$i++ ) {
\$random .= rand( 0, 9 );
}

• I looked at the original post again, and the code there did use the rand() function. I was mistaken about that. Still, I think my solution is better than the original.

• Hey, don't knock interpreted languages. Just the bad ones!

>>> 5 + '9'
Traceback (most recent call last):
File "<pyshell#23>", line 1, in -toplevel-
>>> 5 + '9'
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Of course, there are perverse manipulations of inheritance and type-sniffing that can foil this, but with someone who'd use a trick like that you're screwed anyway.

Remember, interpreted != weak typing, strong typing != static typing, and implementation != specification (or it should be, at least - think of Haskell's GCHi and Hugs).

(NB: Hope this post works, and the Evil Board Software doesn't mangle it.)

• @El Foo said:

Hey, don't knock interpreted languages. Just the bad ones!

>>> 5 + '9'
Traceback (most recent call last):
File "<pyshell#23>", line 1, in -toplevel-
>>> 5 + '9'
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Of course, there are perverse manipulations of inheritance and type-sniffing that can foil this, but with someone who'd use a trick like that you're screwed anyway.

Remember, interpreted != weak typing, strong typing != static typing, and implementation != specification (or it should be, at least - think of Haskell's GCHi and Hugs).

(NB: Hope this post works, and the Evil Board Software doesn't mangle it.)

In Sovjet Javascript, '9' + 5 -> 95
</pyshell#23>

• @dhromed said:

@El Foo said:
Hey, don't knock interpreted languages. Just the bad ones!

>>> 5 + '9'
Traceback (most recent call last):
File "<pyshell #23="">", line 1, in -toplevel-
>>> 5 + '9'
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Of
course, there are perverse manipulations of inheritance and
type-sniffing that can foil this, but with someone who'd use a trick
like that you're screwed anyway.

Remember, interpreted != weak
typing, strong typing != static typing, and implementation !=
specification (or it should be, at least - think of Haskell's GCHi and
Hugs).

(NB: Hope this post works, and the Evil Board Software doesn't mangle it.)

In Sovjet Javascript, '9' + 5 -> 95
</pyshell>

In Soviet Russia, Java scripts you.

• @Quinnum said:

@dhromed said:
@El Foo said:
Hey, don't knock interpreted languages. Just the bad ones!

>>> 5 + '9'
Traceback (most recent call last):
File "<pyshell #23="">", line 1, in -toplevel-
>>> 5 + '9'
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Of
course, there are perverse manipulations of inheritance and
type-sniffing that can foil this, but with someone who'd use a trick
like that you're screwed anyway.

Remember, interpreted != weak
typing, strong typing != static typing, and implementation !=
specification (or it should be, at least - think of Haskell's GCHi and
Hugs).

(NB: Hope this post works, and the Evil Board Software doesn't mangle it.)

In Sovjet Javascript, '9' + 5 -> 95
</pyshell>

In Soviet Russia, Java scripts you.

In Soviet Russa, 9-5 = 24/7

• @dhromed said:

@Quinnum said:

In Soviet Russia, Java scripts you.

In Soviet Russa, 9-5 = 24/7

and Red is the new black. and white. scrap that, red is it.

• @sao said:

@dhromed said:
@Quinnum said:

In Soviet Russia, Java scripts you.

In Soviet Russa, 9-5 = 24/7

and Red is the new black. and white. scrap that, red is it.

In Sovjet Russia, it scraps YOU.

• @FunnyMan said:

@xiphias said:

It's php. It's a number. And a string

Right.  PHP treats "32" and 32 identically.  Ahh, the joys of an interpreted language.
-FM

s/interpreted/weakly typed/ thank you.

Ruby or Python (the Py version was shown above) are interpreted, dynamically typed language, yet they feature strong typing and don't allow that kind of stuff without and explicit conversion, e.g.

```Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on win32
>>> 5+"9"
Traceback (most recent call last):
File "<pyshell#0>", line 1, in -toplevel-
5+"9"
TypeError: unsupported operand type(s) for +: 'int' and 'str'

5+int("9")
14

str(5)+"9"
'59'

```
```irb 0.9(02/07/03)
irb(main):001:0> 5+"9"
TypeError: String can't be coerced into Fixnum
from (irb):1:in `+'
from (irb):1
irb(main):002:0> 5+"9".to_i #
=> 14
irb(main):003:0> 5.to_s+"9"
=> "59"
irb(main):004:0>```

(String#to_i converts a string to an integer, with optional base definition that defaults to 10, and Fixnum#to_s converts an integer to a string, with an optional base definition that also defaults to 10. Calling 5.to_s.to_i is an identity operation, as is calling "5".to_i.to_s)

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.