简体   繁体   中英

How to generate 4 digit random number

Here is what i have tried so far but some times its returning 3 digit or 2 digit or even 1 digit number.

SELECT   num
FROM     GENERATE_SERIES (1, 10000) AS s(num)
order by random()
LIMIT    1

您可以使用此表达式来避免处理子查询

 select floor(1000 + random() * 8999);   

You generate all numbers between 1 and 10000, so 1-999 and 10000 are not 4 digit numbers but in the list:

SELECT   num
FROM     GENERATE_SERIES (1000, 9999) AS s(num) // generates 4 digit numbers
order by random()
LIMIT    1

You can try below

SELECT   num
FROM     GENERATE_SERIES (1000, 9999) AS s(num)
order by random()
LIMIT    1

它将产生从1到10000的值

select to_char(n, 'FM0000') FROM floor(random()*10000) as x(n);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM