Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8332618

Rename the internal os::random() to os::random31bits(), and/or provide os::random_interval(int range) and comment

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Won't Fix
    • Icon: P4 P4
    • tbd
    • None
    • hotspot

      Our os::random() implementation only returns 31 bits, the 32nd bit is always set to 0 because of our choice of implementation:

         * next_rand = (16807*seed) mod (2**31-1)

      If one wants to use it to choose between true/false, one might be tempted to use:

        if (os::random() > 0) {
        } else {
        }

      but that will not work, since the returned value will always be positive.

      We should rename os::random() to os::random31bits(), and/or provide os::random_interval(int range)

      Adding a comment would also be useful.

      Hopefully, this would help to use this API correctly.

            gziemski Gerard Ziemski
            gziemski Gerard Ziemski
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: