Home > Database > Mysql Tutorial > How Can I Generate Random Numbers in SQLite Without Using MySQL's RAND() Function?

How Can I Generate Random Numbers in SQLite Without Using MySQL's RAND() Function?

Patricia Arquette
Release: 2025-01-04 08:14:35
Original
650 people have browsed it

How Can I Generate Random Numbers in SQLite Without Using MySQL's RAND() Function?

SQLite: Alternatives to MySQL's RAND() Function

SQLite lacks a native equivalent to MySQL's RAND() function for generating random numbers. However, there are alternative approaches to achieve similar functionality in SQLite.

One alternative is to use the RANDOM() function, which generates a random number between 0 and 1. While not as versatile as RAND(), it can be used to achieve limited randomization. For instance, the following query selects a random row from the table 'table':

SELECT * FROM table ORDER BY RANDOM() LIMIT 1;
Copy after login

To ensure that the randomness is consistent across different executions, it's recommended to seed the random number generator with a fixed value using the PRAGMA statement before the query:

PRAGMA random_seed(10);
Copy after login

The above is the detailed content of How Can I Generate Random Numbers in SQLite Without Using MySQL's RAND() Function?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template