Here is the TSQL version to reduce rounding using the LINQ to SQL method ExecuteQuery<T>:
List<int> ids = db.ExecuteQuery<int>(@"
declare @n int = {0}, @count int, @offset int, @id int
declare @ids table (pos int identity(1,1) not null, id int not null)
set @count = (select COUNT(1) from Problems)
if @count < @n set @n = @count
while @n > 0
begin
set @offset = 1 + CAST(FLOOR(RAND() * @count) as int)
select @id = (
select Id from (select Id,
ROW_NUMBER() over (order by Id) as __row
from Problems) x where x.__row = @offset)
if not exists (select 1 from @ids where id = @id)
begin
set @n = @n - 1
insert @ids (id) values (@id)
end
end
select id from @ids order by pos", n).ToList();
source
share