Solved

Generating Random Number List

Posted on 2013-11-07
4
446 Views
Last Modified: 2013-11-07
Hi Experts,
I have a one time programing request that I think could be solved with some SQL (also coding in ColdFusion)

Problem:  I need to generate in a random sequence a list if all possible 4 digit numbers only using the set of digits (1,2,3,4,5)

ex: 1234, 4455, 5555, 1111.

I'm not sure how to do this best.  I could
-  Sequentially insert rows with all numbers from 1111 - 5555 (within the set of digits)
-  Assign with it a random number (or use SQL to generate that).

-  Select all rows ordered by random number?

What do you think?

Thanks in advance
hefterr
0
Comment
Question by:hefterr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
Kyle Abrahams earned 250 total points
ID: 39630651
Looks good.


For the random number piece make sure you use new ID as rand will be the same for all rows.

From here:
http://blog.sqlauthority.com/2007/04/29/sql-server-random-number-generator-script-sql-query/

using method 5 on a per row basis, just adjust the range:

SELECT ABS(CAST(NEWID() AS binary(6)) %50000)  + 1 as randomNumber
FROM sysobjects) sample
GROUP BY randomNumber
ORDER BY randomNumber
0
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 250 total points
ID: 39630768
Knocketh yourself out: Click on the below link to go to an article I wrote, and hit Page Down about seven or eight times to get to the part where you can use the RAND() function to make all sorts of randum numbers and characters.
SQL Server:  T-SQL recipe to create a million sample people
0
 
LVL 1

Author Closing Comment

by:hefterr
ID: 39631481
Thanks,
I ended up with something I hacked out as it was a 1 time deal for 625 numbers

DECLARE @table TABLE ( value char(1) )
INSERT INTO @table ( value ) VALUES (1)
INSERT INTO @table ( value ) VALUES (2)
INSERT INTO @table ( value ) VALUES (3)
INSERT INTO @table ( value ) VALUES (4)
INSERT INTO @table ( value ) VALUES (5)

SELECT   t1.value +''+ t2.value +''+ t3.value + '' + t4.value AS theNumber
FROM      @table t1
                 CROSS JOIN @table t2
                 CROSS JOIN @table t3
                 CROSS JOIN @table t4
order by newid()
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39631511
Good deal.  Btw, save you some typing...

INSERT INTO @table ( value ) VALUES (1), (2), (3), (4), (5)
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question