[Django] #32060: Expose Random() as a function instead of an expression.

19 views
Skip to first unread message

Django

unread,
Oct 1, 2020, 5:21:37 AM10/1/20
to django-...@googlegroups.com
#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.

Django

unread,
Oct 1, 2020, 5:32:39 AM10/1/20
to django-...@googlegroups.com
#32060: Expose Random() as a function instead of an expression.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: | Status: assigned
Cleanup/optimization |
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:

Keywords: random, function. | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Nick Pope):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/13473 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/32060#comment:1>

Django

unread,
Oct 1, 2020, 5:39:14 AM10/1/20
to django-...@googlegroups.com
#32060: Expose Random() as a function instead of an expression.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: New feature | Status: assigned

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: random, function. | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* type: Cleanup/optimization => New feature
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/32060#comment:2>

Django

unread,
Oct 2, 2020, 1:01:12 AM10/2/20
to django-...@googlegroups.com
#32060: Expose Random() as a function instead of an expression.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: New feature | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: random, function. | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/32060#comment:3>

Django

unread,
Oct 2, 2020, 2:11:10 AM10/2/20
to django-...@googlegroups.com
#32060: Expose Random() as a function instead of an expression.
-------------------------------------+-------------------------------------
Reporter: Nick Pope | Owner: Nick Pope
Type: New feature | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: random, function. | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"06c5d3fafc6aeb96387148726713b611aeba7fa1" 06c5d3f]:
{{{
#!CommitTicketReference repository=""
revision="06c5d3fafc6aeb96387148726713b611aeba7fa1"
Fixed #32060 -- Added Random database function.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/32060#comment:4>

Reply all
Reply to author
Forward
0 new messages