Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Generating random numbers in a range, sql2k

By hjm, 2004/10/08

This script will generated random numbers within a choosen Upper and LowerBound.

I'v created a random number (int's) generator since i couldnt find a script which exactly did what I was searching for. The RAND function seems to do repetive patterns, so i created a new function based on the NEWID function.

After running the two create functions, call the randomize function with:
select master.dbo.fx_generateRandomNumber(newID(), 5, 10)
5 = the lowerbound and 10 the upperbound of the range.
Use always the newid() as first param since this will guarantee uniqueness. I could not use it inside the generate function due too sql scalar function limitations.

henk-nospam-@hatchlab.nl

Total article views: 337 | Views in the last 30 days: 3
 
Related Articles
ARTICLE

Randomizing Result Sets with NEWID

Seth Delconte brings us a technique to solve a common request. Using the NEWID function to return a ...

ARTICLE

Generating Non-uniform Random Numbers with SQL

Some FUNCTIONs to help generate non-uniform random numbers from uniform random numbers including Nor...

FORUM

Random number generator

Random numbers

BLOG

Random Ordering of Results using NEWID

A discussion of how NEWID can be used to help randomize the results returned from a SQL query. A ...

FORUM

SQL Random selection with NewID()

How to randomly select questions from SQL Server database onto a page Order by NewID().

Tags
t-sql    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones