是否可以在 MS Access 中生成每次运行查询时都不会改变的随机数?
我正在尝试从 Microsoft Access 数据库中随机提取 200 条记录。我创建了一个查询,并在新字段中使用 rnd() 函数为每条记录生成一个随机数。然后我对随机数字段进行排序并检索了前 200 篇文章。我的问题是,每次运行查询时,我都会得到不同的随机数。我知道在其他程序中,您可以为种子设置用户定义的值,并且此问题将得到解决,但是我不知道如何在 MS Access 中自己设置种子值。这可以吗?
感谢您的帮助!
I am trying to pull a random set of 200 records from my Microsoft Access database. I created a query and used the rnd() function in a new field to generate a random number for each record. I then sorted the random number field and retrieved the top 200 articles. My problem is that every time I run the query, I get different random numbers. I know that in other programs, you can set a user-defined value for the seed, and this problem will be fixed, however I do not know how to set the seed value myself in MS Access. Is this possible to do?
Thank you for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否尝试阅读 Rnd() 函数的 Access 帮助?该函数有一个参数,帮助文件定义了种子的作用:
因此,您希望在每次运行查询时传递一个对每行都相同的负数。自动编号 PK 将是一个理想的候选者,因为它是唯一的 - 只需使用
Rnd(-MyPK)
。唯一的问题是,如果您使用随机自动编号,在这种情况下,某些数字已经是负数,因此您会遇到问题。我没有一个好的解决方案来避免冲突(即,两行产生相同的值,因为它们具有相同的绝对值)并且不会导致溢出。
Did you try reading the Access help for the Rnd() function? The function has a single argument, and the help file defines what the seed does:
So, you want to pass a negative number that is the same for each row each time you run the query. An Autonumber PK would be an ideal candidate for that, since it will be unique -- just use
Rnd(-MyPK)
.The only problem would be if you are using random Autonumbers, in which case some of the numbers would be negative already, so you'd have a problem there. I don't have a good solution to that that would avoid collisions (i.e., two rows producing the same value because they have the same absolute value) and that wouldn't lead to an overflow.
在取负数之前简单地对自动数进行平方即可消除“已经是负数”的问题?
Simply square the autonumber before taking the negative to remove the "already negative" problem?