Seeding SQL RAND() Method With NEWID() For Per-Row Random Values (Thanks Joshua Cyr)

Posted January 27, 2009 at 2:54 PM

Tags: SQL

From what I have read and from what I've seen demos of, I thought that MS SQL's RAND() method could only return one random value per connection. As I never looked at the documentation for RAND(), I never realized that it could be seeded. This morning, in my post on selecting random records from a weighted data table, Joshua Cyr pointed me to a great blog post demonstrating how to get RAND() to produce random numbers for each record in a query.

Before we see that, however, let's take a look at how it won't work:

 Launch code in new window » Download code as text file »

  • <!--- Select random numbers. --->
  • <cfquery name="qRandom" datasource="#REQUEST.DSN#">
  • SELECT
  • p.id,
  • ( RAND() ) AS random_number
  • FROM
  • pivot1000 p
  • WHERE
  • p.id < 10
  • ORDER BY
  • p.id ASC
  • </cfquery>
  •  
  •  
  • <!--- Output the random numbers generated. --->
  • <cfloop query="qRandom">
  •  
  • #qRandom.random_number#<br />
  •  
  • </cfloop>

Notice here that we are simply selecting RAND() as a column in the table, then output that column. When we run this code, we get the following:

0.625088599784
0.625088599784
0.625088599784
0.625088599784
0.625088599784
0.625088599784
0.625088599784
0.625088599784
0.625088599784

Each call to RAND() produces the same result. This is because the RAND() method is implicitly seeded on the first call and then each subsequent call in the same connection uses the same seed value. To get a new random value for each RAND() method call, we have to pass it a new seed value each time.

The seed value must be numeric. Now, in the past, I've used NEWID() to create random numbers; but again, I never tried this on a per-row basis. As demonstrated in Josh's link, the NEWID() can be used to seed the RAND() method call every time it is run:

 Launch code in new window » Download code as text file »

  • <!--- Select random numbers. --->
  • <cfquery name="qRandom" datasource="#REQUEST.DSN#">
  • SELECT
  • p.id,
  •  
  • <!--- Seed RAND() with binary version of NEWID(). --->
  • (
  • RAND(
  • CAST( NEWID() AS varbinary )
  • )
  • ) AS random_number
  • FROM
  • pivot1000 p
  • WHERE
  • p.id < 10
  • ORDER BY
  • p.id ASC
  • </cfquery>
  •  
  •  
  • <!--- Output the random numbers generated. --->
  • <cfloop query="qRandom">
  •  
  • #qRandom.random_number#<br />
  •  
  • </cfloop>

As I have stated before, I still don't know what the conversion from UUID to VARBINARY does exactly, but it produces are binary data structure that then get implicitly cast to a numeric value when passed to the RAND() method. As a result, this seeds the RAND() method with a new value on each row and gives us the following output:

0.632351429673
0.277469709628
0.291371676631
0.187895386945
0.0332443201662
0.269721718406
0.801980914521
0.984558916944
0.569620907931

A random value for each record. Good stuff! Thanks a lot Josh!

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Jan 27, 2009 at 8:42 PM // reply »
29 Comments

just in case you don't already know you can also use NEWID() to return random records from a table by using it in the ORDER BY clause:

ORDER BY NEWID()


Jun 23, 2009 at 12:12 PM // reply »
1 Comments

you are they man. Great idea of of unique row values selection.


Oct 7, 2009 at 1:25 PM // reply »
1 Comments

Thanks - this post helped me out :)


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »
Nov 21, 2009 at 10:56 AM
HostMySite.com Has The Best ColdFusion Hosting
@Mehul, Yes very nice people, however several downtimes per day which was not acceptable. Hence we had to move out. I am glad you are having good luck with them so far. ... read »
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »