Opened 4 years ago

Closed 4 years ago

#32060 closed New feature (fixed)

Expose Random() as a function instead of an expression.

Reported by: Nick Pope Owned by: Nick Pope
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: random, function.
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

This follows on from #31839 where all other proposals were rejected, but exposing Random() as a function was 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 RANDOM double precision [0.0, 1.0)
MySQL RAND double precision [0.0, 1.0)
MariaDB RAND double precision [0.0, 1.0)
Oracle DBMS_RANDOM.RANDOM integer [-2^31, 2^31) Currently used in Django, but Oracle have deprecated it.
Oracle DBMS_RANDOM.VALUE number [0.0, 1.0)
SQLite 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.

Change History (4)

comment:1 by Nick Pope, 4 years ago

Has patch: set

comment:2 by Mariusz Felisiak, 4 years ago

Triage Stage: UnreviewedAccepted
Type: Cleanup/optimizationNew feature

comment:3 by Mariusz Felisiak, 4 years ago

Triage Stage: AcceptedReady for checkin

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 06c5d3f:

Fixed #32060 -- Added Random database function.

Note: See TracTickets for help on using tickets.
Back to Top