<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SQL Fool &#187; random number</title>
	<atom:link href="http://sqlfool.com/tag/random-number/feed/" rel="self" type="application/rss+xml" />
	<link>http://sqlfool.com</link>
	<description>Adventures in SQL Tuning - a blog for the rest of us</description>
	<lastBuildDate>Wed, 02 Nov 2011 20:39:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Random Number Generator in T-SQL</title>
		<link>http://sqlfool.com/2009/06/random-number-generator-in-tsql/</link>
		<comments>http://sqlfool.com/2009/06/random-number-generator-in-tsql/#comments</comments>
		<pubDate>Thu, 04 Jun 2009 13:21:17 +0000</pubDate>
		<dc:creator>Michelle Ufford</dc:creator>
				<category><![CDATA[Performance & Tuning]]></category>
		<category><![CDATA[SQL Tips]]></category>
		<category><![CDATA[Syndication]]></category>
		<category><![CDATA[random number]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://sqlfool.com/?p=489</guid>
		<description><![CDATA[Ever need to generate a random number in T-SQL? I have, on a couple of different occasions. I'm pretty sure that there's several different ways of doing this in T-SQL, but here's what I use: Declare @maxRandomValue tinyint = 100 , @minRandomValue tinyint = 0; &#160; Select Cast&#40;&#40;&#40;@maxRandomValue + 1&#41; - @minRandomValue&#41; * Rand&#40;&#41; + [...]]]></description>
			<content:encoded><![CDATA[<p>Ever need to generate a random number in T-SQL?  I have, on a couple of different occasions.  I'm pretty sure that there's several different ways of doing this in T-SQL, but here's what I use:</p>

<div class="wp_syntax"><div class="code"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">Declare</span> @maxRandomValue <span style="color: #0000FF;">tinyint</span> <span style="color: #808080;">=</span> <span style="color: #000;">100</span>
	, @minRandomValue <span style="color: #0000FF;">tinyint</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span>;
&nbsp;
<span style="color: #0000FF;">Select</span> <span style="color: #0000FF;">Cast</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#40;</span>@maxRandomValue <span style="color: #808080;">+</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">-</span> @minRandomValue<span style="color: #808080;">&#41;</span> 
	<span style="color: #808080;">*</span> <span style="color: #FF00FF;">Rand</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">+</span> @minRandomValue <span style="color: #0000FF;">As</span> <span style="color: #0000FF;">tinyint</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">As</span> <span style="color: #FF0000;">'randomNumber'</span>;</pre></div></div>

<p>This approach uses the <a href="http://msdn.microsoft.com/en-us/library/ms177610.aspx" target="_blank">RAND()</a> function to generate a random seed; it also ensures that the value returned is between the specified min and max value.  I've been using this method in one stored procedure that's called a couple of hundred times per second, and it seems to perform pretty well.</p>
<p>What method do YOU use to generate a random number?  Is it faster than this method?  </p>
]]></content:encoded>
			<wfw:commentRss>http://sqlfool.com/2009/06/random-number-generator-in-tsql/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

