Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 121555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:59:21+00:00 2026-05-11T03:59:21+00:00

I would like to randomly sort a result in a repeatable fashion for purposes

  • 0

I would like to randomly sort a result in a repeatable fashion for purposes such as paging. For this NEWID() is too random in that the same results cannot be re-obtained. Order by Rand(seed) would be ideal as with the same seed the same random collection would result. Unfortunately, the Rand() state resets with every row, does anyone have a solution?

declare @seed as int; set @seed = 1000;  create table temp ( id int, date datetime)  insert into temp (id, date) values (1,'20090119') insert into temp (id, date) values (2,'20090118') insert into temp (id, date) values (3,'20090117') insert into temp (id, date) values (4,'20090116') insert into temp (id, date) values (5,'20090115') insert into temp (id, date) values (6,'20090114')  -- re-seeds for every item select *, RAND(), RAND(id+@seed) as r from temp order by r --1 2009-01-19 00:00:00.000 0.277720118060575   0.732224964471124 --2 2009-01-18 00:00:00.000 0.277720118060575   0.732243597442382 --3 2009-01-17 00:00:00.000 0.277720118060575   0.73226223041364 --4 2009-01-16 00:00:00.000 0.277720118060575   0.732280863384898 --5 2009-01-15 00:00:00.000 0.277720118060575   0.732299496356156 --6 2009-01-14 00:00:00.000 0.277720118060575   0.732318129327415 -- Note how the last column is +=~0.00002  drop table temp  -- interestingly this works: select RAND(@seed), RAND() --0.732206331499865 0.306382810665955 

Note, I tried Rand(ID) but that just turns out to be sorted. Apparently Rand(n) < Rand(n+1)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T03:59:22+00:00Added an answer on May 11, 2026 at 3:59 am

    Building off of gkrogers hash suggestion this works great. Any thoughts on performance?

    declare @seed as int; set @seed = 10;  create table temp ( id int, date datetime)  insert into temp (id, date) values (1,'20090119') insert into temp (id, date) values (2,'20090118') insert into temp (id, date) values (3,'20090117') insert into temp (id, date) values (4,'20090116') insert into temp (id, date) values (5,'20090115') insert into temp (id, date) values (6,'20090114')  -- re-seeds for every item select *, HASHBYTES('md5',cast(id+@seed as varchar)) r from temp order by r --1 2009-01-19 00:00:00.000 0x6512BD43D9CAA6E02C990B0A82652DCA --5 2009-01-15 00:00:00.000 0x9BF31C7FF062936A96D3C8BD1F8F2FF3 --4 2009-01-16 00:00:00.000 0xAAB3238922BCC25A6F606EB525FFDC56 --2 2009-01-18 00:00:00.000 0xC20AD4D76FE97759AA27A0C99BFF6710 --3 2009-01-17 00:00:00.000 0xC51CE410C124A10E0DB5E4B97FC2AF39 --6 2009-01-14 00:00:00.000 0xC74D97B01EAE257E44AA9D5BADE97BAF  drop table temp 

    EDIT: Note, the declaration of @seed as it’s use in the query could be replace with a parameter or with a constant int if dynamic SQL is used. (declaration of @int in a TSQL fashion is not necessary)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For each parent that exists I would like to select 1 child row randomly.
I would like to sort an array in ascending order using C/C++ . The
I would like to use a language that I am familiar with - Java,
So, I was thinking about making a simple random world generator. This generator would
I would like to write a context_processor, something like this: settings.py: MEDIA_URLS = ('cname2.example.com/media',
Would like to get a list of advantages and disadvantages of using Stored Procedures.
Would like to create a strong password in C++. Any suggestions? I assume it
I would like to test a string containing a path to a file for
I would like to have a reference for the pros and cons of using
I would like to filter an array of items by using the map() function.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.