#32060: Expose Random() as a function instead of an expression.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: | Status: assigned
Cleanup/optimization |
Component: Database | Version: master
layer (models, ORM) |
Severity: Normal | Keywords: random, function.
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
This follows on from #31839 where all other proposals were rejected, but
exposing `Random()` as a function was
[https://code.djangoproject.com/ticket/31839#comment:6 accepted].
I also propose unifying the random number returned to be in the range
`[0.0, 1.0)` to make it more useful as the backend support differs:
||=**Backend** =||=**Function** =||=**Type** =||=**Range** =||=**Notes**
=||
|| PostgreSQL || [https://www.postgresql.org/docs/current/functions-
math.html#FUNCTIONS-MATH-RANDOM-TABLE RANDOM] || double precision ||
`[0.0, 1.0)` ||||
|| MySQL || [https://dev.mysql.com/doc/refman/8.0/en/mathematical-
functions.html#function_rand RAND] || double precision || `[0.0, 1.0)`
||||
|| MariaDB || [https://mariadb.com/kb/en/rand/ RAND] || double precision
|| `[0.0, 1.0)` ||||
|| Oracle || [https://docs.oracle.com/en/database/oracle/oracle-
database/19/arpls/DBMS_RANDOM.html#GUID-F401154C-
8A96-4634-93F9-BC4A8B52F9A7 DBMS_RANDOM.RANDOM] || integer || `[-2^31,
2^31)` || Currently used in Django, but Oracle have deprecated it. ||
|| Oracle || [https://docs.oracle.com/en/database/oracle/oracle-
database/19/arpls/DBMS_RANDOM.html#GUID-AAD9E936-D74F-440D-
9E16-24F3F0DE8D31 DBMS_RANDOM.VALUE] || number || `[0.0, 1.0)` ||||
|| SQLite || [https://sqlite.org/lang_corefunc.html#random RANDOM] ||
integer || `[-2^63, 2^63)` || We can use Python's `random.random()` for
`[0.0, 1.0)`||
This shouldn't adversely affect existing use cases of the expression as it
was undocumented and used to support `.order_by('?')`.
We should steer away from setting the random seed as it varies wildly
between backends.
We should also avoid any backend-specific arguments for the upper and
lower limits for the random number.
For anyone arriving here that wants to generate a random integer from a
random number in the range `[0.0, 1.0)` see the MySQL documentation for a
good example.
--
Ticket URL: <https://code.djangoproject.com/ticket/32060>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/050.15d2fbf577c26da30b2fc0f5dba1ab5e%40djangoproject.com.