[wp-trac] [WordPress Trac] #55194: wp_rand( 0, 0) result in random integer

WordPress Trac noreply at wordpress.org
Fri Feb 18 10:40:34 UTC 2022


#55194: wp_rand( 0, 0)  result in random integer
--------------------------+---------------------
 Reporter:  mlajo         |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  6.0
Component:  General       |     Version:  5.9
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:  docs
--------------------------+---------------------
Changes (by SergeyBiryukov):

 * focuses:   => docs
 * milestone:  Awaiting Review => 6.0


Old description:

> wp_rand( 0, 0)  result in random integer instead of 0

New description:

 wp_rand( 0, 0)  result in random integer instead of 0

--

Comment:

 Replying to [comment:1 azouamauriac]:
 > I'm able to reproduce the issue, the function was introduce here [8728].
 For what I understand it used to replace ''mt_rand'' RE
 [https://www.php.net/manual/en/function.mt-rand.php].
 > but "why" i don't know.

 You can find more details in the
 [https://wordpress.org/news/2008/09/wordpress-262/ WordPress 2.6.2 release
 post].

 Looks like the current behavior is intentional, see for example this line
 added in [34981] / #28633:
 {{{
 $_max = ( 0 != $max ) ? $max : $max_random_number;
 }}}

 This is also consistent with `mt_rand()`, which returns a random integer
 and not 0 when parameters are omitted. So changing this behavior does not
 seem feasible due to security implications, but we can update the
 documentation to mention that the `$max` value is only taken into account
 if it's greater than zero.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/55194#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list