SqlShare.com Logo
 
Skip Navigation Links
Home
Video List
Events
Classes
About Us
Login / Register
Subscribe RSS Feed 

Takes Query Plan Analysis and Performance Monitoring to a New Level

Using the RAND Function

Need to generate a random number? The RAND function is the TSQL tool for the job! What about needing to generate the same random number each time for a given seed? RAND can do that too. We'll do a short demo of both techniques so that if the boss calls for a random number - perhaps to decide how much of a raise you'll get - you'll be ready to go!

Duration:
2 mins 6 secs
Skill Level:
100
Rating:
4.08 out of 5
Publish Date:
November 20, 2010
Using the RAND Function You must be logged in to view this video.  
Bookmark and Share
 
1=Poor, 3=Good, 5=Excellent

About the Author

Image of Andy Warren
Andy Warren is a software trainer focusing on SQL Server, a member of the PASS Board of Directors, and a principal in this site - SQLShare.com.

References



Comments
Steve Culshaw on 11/23/2010
Good concise overview of RAND

Rimjim on 11/23/2010
very good tip

Sainey M. Drammeh on 11/23/2010
Thank you Andy.

lakshmi on 11/23/2010
simple and good..

Tahir A. Syed on 11/23/2010
Daily hints is a simple reminder & improve our knowledge, thanks...Tahir

C Boos on 11/23/2010
Try rand(rand()). You'll see that it still acts as a seed and the result does not change even though you would expect the internal rand() to generate a new seed each time

Mike Jackson on 11/23/2010
Hey Andy, One of the questions I get alot is, how to get a integer between an upper and lower range, like 1 and 1000. If you do select rand()*1000 you will get something like 124.114808284108 so if you cast that as an integer you will get something like... select cast(rand()*1000 as int) 124 Now you need to add 1 to it or you will possibly get a zero and never achive the upper limit. select cast(rand()*1000 as int)+1 So if you need a 1000 random numbers just put that a loop and let it run.

dfortier on 11/23/2010
Hi Andy, I have a monthly job that makes use of the RAND function. Our helpdesk does a monthly self-audit of 20 random network accounts. To accomplish this I read the Active Directory accounts and place the names in a table with an identity column that begins at one and increments by one. Subsequently I use a 20 iteration WHILE loop and the RAND function to randomly read 20 employees from that table. Works very well. Thanks for all the great videos. df

DaveClark on 11/23/2010
Interesting comment above by C Boos on RAND(RAND()). Anyone know why that is?

Nilesh on 11/23/2010
GOOD didnt know tht sql could do tht !!!

Jack Hellmuth on 11/23/2010
Is the rand funtion without a seed actually implemented with a system seed?

kevin on 11/23/2010
decent, would have like to see you put in example of how to get larger random numbers between a scale.

Dennis Allen on 11/23/2010
How about a key exchange for one time pad generation. You could generate and share some random seed DECLARE @secret int; SET @secret = cast(cast(newid() AS varbinary(4)) AS int); And then use the RAND() function to generate key sets, perhaps building key tables in 8 byte blocks cast( rand()*9223372036854775807-9223372036854775808 AS bigint ) and then assembling those into 512 byte keys or what not. There are better ways... but you could do that.

WChaster on 11/23/2010
I did not know this.

Leonard Peoples on 11/23/2010
Very cool

Syed Arshad on 11/23/2010
Could not catch the catch

Gil on 11/23/2010
Might have been good to describe the purpose of the seed value (to generate a repeatable sequence of random numbers). Also, it might be good to discuss good expressions for the seed to generate unpredictable random sequences.

SCOTT SNYDER on 11/23/2010
The video got me started and thinking about how to use the function. I did some playing with it and research to come up with a way to autopopulate test data. By putting in the test data I was able to have groups test their business calucations and queries until I was ready to go with the actual data.

Ivan Arturo Valle on 11/23/2010
Excellent tips

Faisal Lodhi on 11/23/2010
Thanks you

manjeet on 11/23/2010
Nice concept for random number

Rajiv Gupta on 11/24/2010
Very good!!!

Patrick on 11/24/2010
That's a great video, like the Go 10 trick, but what you do with Rand()?

I've use RAND to generate random numeric data (along with some additional arithmetic functionality to define a range of numeric data).

Jacquelyn Knapp on 12/3/2010
So could you use this as a random generator for a quarterly review?

Sean P. Murphy on 12/9/2010
good examples of rand

Ed K on 12/29/2010
should point out that select *,rand() from any_table will return same random number with each row. This prevents using it to select random records

ravi on 4/17/2011
nice

T. Carnahan on 4/27/2011
Can you show an example of how to get a different output every time (truely random)?



Must Be Logged In
 

How Do I Become a Video Author? |  Newsletter History

Copyright © Fourdeuce, Inc., 2005-2009. All Rights Reserved | Privacy Policy | Terms & Conditions