使用 MYSQL 创建随机数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14798640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:30:08  来源:igfitidea点击:

creating a random number using MYSQL

mysqlsql

提问by TNK

I would like to know is there a way to select randomly generated number between 100 and 500 along with a select query.

我想知道有没有办法选择 100 到 500 之间随机生成的数字以及选择查询。

Eg: SELECT name, address, random_number FROM users

例如: SELECT name, address, random_number FROM users

I dont have to store this number in db and only to use it to display purpose.

我不必将此数字存储在 db 中,只需将其用于显示目的。

I tried it something like this, but it can't get to work..

我试过这样的事情,但它无法开始工作..

SELECT name, address, FLOOR(RAND() * 500) AS random_number FROM users

Hope someone help me out. Thank you

希望有人帮助我。谢谢

回答by Ja?ck

This should give what you want:

这应该给你想要的:

FLOOR(RAND() * 401) + 100

Generically, FLOOR(RAND() * (<max> - <min> + 1)) + <min>generates a number between <min> and <max>inclusive.

通常,FLOOR(RAND() * (<max> - <min> + 1)) + <min>生成一个介于<min> 和<max>包含的数字之间。

Update

更新

This full statement should work:

这个完整的声明应该有效:

SELECT name, address, FLOOR(RAND() * 401) + 100 AS `random_number` 
FROM users

回答by Ed Heal

As RANDproduces a number 0 <= v < 1.0 (see documentation) you need to use ROUNDto ensure that you can get the upper bound (500 in this case) and the lower bound (100 in this case)

AsRAND产生一个数字 0 <= v < 1.0 (参见文档),您需要使用它ROUND来确保您可以获得上限(在这种情况下为 500)和下限(在这种情况下为 100)

So to produce the range you need:

所以要产生你需要的范围:

SELECT name, address, ROUND(100.0 + 400.0 * RAND()) AS random_number
FROM users

回答by Kadir Erturk

Additional to this answer, create a function like

除了这个答案,创建一个函数

CREATE FUNCTION myrandom(
    pmin INTEGER,
    pmax INTEGER
)
RETURNS INTEGER(11)
DETERMINISTIC
NO SQL
SQL SECURITY DEFINER
BEGIN
  RETURN floor(pmin+RAND()*(pmax-pmin));
END; 

and call like

并打电话给

SELECT myrandom(100,300);

SELECT myrandom(100,300);

This gives you random number between 100 and 300

这为您提供了 100 到 300 之间的随机数

回答by KhalilPan

these both are working nicely:

这两个都运行良好:

select round(<maxNumber>*rand())


FLOOR(RAND() * (<max> - <min> + 1)) + <min> // generates a number
between <min> and <max> inclusive.

回答by bonCodigo

You could create a random number using FLOOR(RAND() * n) as randnum(n is an integer), however if you do not need the same random number to be repeated then you will have to somewhat store in a temp table. So you can check it against with where randnum not in (select * from temptable)...

您可以使用FLOOR(RAND() * n) as randnum(n 是整数)创建一个随机数,但是如果您不需要重复相同的随机数,那么您将不得不存储在临时表中。所以你可以用where randnum not in (select * from temptable)......

回答by ???? ????

This is correct formula to find integers from ito jwhere i <= R <= j

这是正确的公式,找到整数ij哪里i <= R <= j

FLOOR(min+RAND()*(max-min))