简体   繁体   English

如何随机播种 MySQL 列?

[英]How can I random seed a MySQL column?

The column in my database contains 4 statuses:我的数据库中的列包含 4 个状态:

  • 0-Active 0-主动
  • 1-Awaiting User input 1-等待用户输入
  • 2-Archive In Progress 2-归档中
  • 3-Excluded From Archival 3-从档案中排除

I need to go through the column and randomly change things that are not marked as 0 to some random picking of the other 3 statuses.我需要浏览该列并将未标记为 0 的内容随机更改为其他 3 个状态的随机选择。

What would be the best way for me to go about this?对我来说,最好的方法是什么? It was requested I use a Python script with random seed so that the state could be recreated.有人要求我使用带有随机种子的 Python 脚本,以便可以重新创建状态。 Does anyone have thoughts?有没有人有想法?

I don't really see the point using python here, you can adress the very same requirement with a pure MySQL query, like so:我真的不明白在这里使用 python 的意义,您可以使用纯 MySQL 查询满足相同的要求,如下所示:

update mytable set mystatus = floor(1 + rand() * 3) where mystatus <> 0

For each row, expression floor(1 + rand() * 3) gives you a random value between 1 and 3.对于每一行,表达式floor(1 + rand() * 3)为您提供 1 到 3 之间的随机值。

As for the seed, the documentation says :至于种子, 文档说

If an integer argument N is specified, it is used as the seed value:如果指定了整数参数N ,则将其用作种子值:

  • With a constant initializer argument, the seed is initialized once when the statement is prepared, prior to execution.使用常量初始化参数,在准备语句时,在执行之前,种子会被初始化一次。

  • With a nonconstant initializer argument (such as a column name), the seed is initialized with the value for each invocation of RAND() .使用非常量初始值设定项参数(例如列名),种子被初始化为每次调用RAND()

One implication of this behavior is that for equal argument values, RAND(N) returns the same value each time, and thus produces a repeatable sequence of column values.这种行为的一个含义是,对于相等的参数值, RAND(N)每次都返回相同的值,从而产生一个可重复的列值序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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