T-SQL Randomization; giving developers the power to randomize the world

by alexi on May 10, 2010

T-SQL Randomization - giving developers the power to radomize the world...

Developers are always looking for new ways to make their jobs easier. Luckily, the software developers are listening; complex, time-consuming and confusing tasks in flash, CSS, .Net etc. have often been transformed into simple lines of code.

The functionality of T-SQL is no different; now regularly used since the 2005 release, T-SQL offers more options to transform complex tasks. Plus, the newly developed randomization tool is a cool way to add a bit of extra fun to your results. This article shows you how it works.

Although order is very important, and many users like to make sure their data is alphabetized, or numerical, randomization shouldn’t be ignored. Developers are often adding randomization to their programmes, whether they want to order by name, pull a unique number for products or just spice up a website.

This need has given T-SQL creators the aim of making randomization easier, and they have achieved it through NEWID()!

NEWID() generates a new random Globally Unique Identifier (GUID for short) using whatever record you need.

To randomly order by name just add a column to your query containing the result from NEWID() and order by this column. For example:

SELECT [Name], [Address], [EmailAddress] FROM [MyTable] ORDER BY [Name]

Always pulls the records in name order

SELECT [Name], [Address], [EmailAddress],NEWID() as [Randomness] FROM [MyTable]
ORDER BY [Randomness]

Being able to generate a random product number is great for companies with large product lines who need to ensure ISBN’s are not repeated, the same process will grab a unique number from 1 of 1632 combinations, approximately 10,000,000,000,000,000,000,000,000,000,000,000,000 different codes – pretty amazing!

Try producing your own GUID’s with the online GUID generator, this handy tool lets the user generate their own GUID in real time and provides them with an IEE 802 hardware address which should be unique.

Of course, the problem of GUID’s is the question of true randomness. It is really hard to tell how the numbers are being generated as there are several different algorithms being used and whether the system repeats itself. Still most people consider GUIDs to be secure enough to

The problem with having numbers for everyone, and everything, is that this greater possibility of replication could potentially ruin a process. Users could get around this by moving away from GUID’s, and working with IPv6 (an alternative data generator), as there is a central authority that manages the data, ensuring no repetition is possible.

In reality, unless everyone decided to divide all their possessions using GUID then they tried to sell them on the same site or tried to collate on a mega spreadsheet, then there would be a need for concern. But as there is 1 in 1036 chance of repetition, I am sure that users will chance it!

In reality, unless everyone decided to divide all their possessions using GUID then they tried to sell them on the same site or tried to collate on a mega spreadsheet, then there would be a need for concern. But as there is 1 in 1036 chance of repetition, I am sure that users will chance it!

Author : Alex Iszatt

Digital Journalist at Creative Jar; working with a whole bunch of creative, technical and intelligent people… a big, happy team with the same goal – to make the client happy!

Follow CreativeJar on Twitter to get my daily ramblings

{ 4 comments… read them below or add one }

Jason P May 11, 2010 at 4:14 pm

Interesting article on a not so interesting subject.

Reply

Christian Caseman May 24, 2010 at 3:22 pm

Thanks for your great work!

Reply

Martijn Kruithof May 27, 2010 at 8:59 pm

A type 1 UUID is not random, but mostly time based, furthermore, it does contain an IEE 802 hardware address which should be unique, as these ID’s are handed out to hardware vendors who must not re-use the same address. An UUID cannot be used as IEE 802 hardware address (it does not provide such addresses).

The UUID in the picture above was generated Monday, May 10, 2010 3:20:46 PM GMT.

Type 4 UUID’s are based on random numbers. If everyone plays by the rules uniqueness of type 1 UUID’s can be guaranteed. Uniqueness of type 4 UUID’s cannot be guaranteed.

Reply

learn hindi online May 29, 2010 at 4:25 am

thank you. Come back soon.

Reply

Leave a Comment

{ 2 trackbacks }

Previous post:

Next post: