为什么 RAND() 不产生随机数? [英] Why is RAND() not producing random numbers?

查看:29
本文介绍了为什么 RAND() 不产生随机数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在回答一个问题hereabouts 并且发火了我的 SSMS 在发布之前测试了一个小查询,但得到了一些奇怪的结果.这是查询:

I was answering a question hereabouts and fired up my SSMS to test a little query before posting it, but got some strange results. Here's the query:

UPDATE Person
SET   Pos_X = Rand()
    , Pos_Y = Rand(id)

SELECT ID, Surname, Forename, Pos_X, Pos_Y FROM Person

这是结果集:

1   Bloggs  Fred    0.332720913214171   0.713591993212924
2   Doe     Jane    0.332720913214171   0.713610626184182
3   Smith   Mary    0.332720913214171   0.71362925915544
4   Jones   Martha  0.332720913214171   0.713647892126698
5   Jones   Martha  0.332720913214171   0.713666525097956
6   Jones   Martha  0.332720913214171   0.713685158069215
7   Jones   Martha  0.332720913214171   0.713703791040473
8   Jones   Martha  0.332720913214171   0.713722424011731
9   Jones   Martha  0.332720913214171   0.713741056982989

正如我所期望的,没有种子的 Rand 在每一行中都会得到相同的结果,但我希望带有种子的 rand(只是数字 1 到 9)会比彼此相差 0.0002 以内的有序列表好一点!

As I expected Rand without a seed put the same result in each row, but I was hoping that rand with a seed (albiet just the numbers 1 to 9) would do a bit better than an ordered list within 0.0002 of each other!

你也一样吗?这听起来像是可以抓住粗心的人.

Do you get the same? This sounds like something that could catch out the unwary.

因为我确信这将是相关的:

As I'm sure this will be relevant:

@@Version = 'Microsoft SQL Server 2005 - 9.00.5000.00 (Intel X86)   Dec 10 2010 10:56:29   Copyright (c) 1988-2005 Microsoft Corporation  Express Edition on Windows NT 6.1 (Build 7601: Service Pack 1)  '

推荐答案

RAND (事务 SQL):

返回一个从 0 到 1 的伪随机浮点值,不包括.

Returns a pseudo-random float value from 0 through 1, exclusive.

还有:

使用相同种子值重复调用 RAND() 返回相同结果.

Repetitive calls of RAND() with the same seed value return the same results.

(强调我的)

这篇关于为什么 RAND() 不产生随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆