24 August 2007 11:57 Alex_Kuznetsov

Selecting a random number for each row


create view wrapped_rand_view
as
select rand( ) as random_value
go
create function wrapped_rand()
returns float
as
begin
declare @f float
set @f = (select random_value from wrapped_rand_view)
return @f
end

select --your columns--, dbo.wrapped_rand() wrapped_rand
from YourTable

Comments

# re: Selecting a random number for each row

08 September 2007 12:45 by zzzbla

Hey,

I usually use this to get random values (per row):

select abs(checksum(newid())) % @upper_boundary

from yourtable

(I usually use this when I fill tables with data for stress testing, so it usually looks like

SELECT TOP (@num_of_rows) abs(checksum(newid())) % @upper_boundary

FROM master..sysobjects a CROSS JOIN master..sysobjects b)

It seems like it outperforms your method, but the two methods should be tested on a machine better than my poor laptop.