Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Generating a set of semi-random numbers Expand / Collapse
Author
Message
Posted Tuesday, July 9, 2013 6:54 PM
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Today @ 12:47 PM
Points: 710, Visits: 4,541
I can generate random numbers just fine using NewID(). I got it from SqlAuthority.com, works a charm.

I am generating numbers between 1 and 5. If I just use NewID() or similar, I will likely get an even spread of values, which I don't want. Instead, I wanted the frequency to decrease as the number increased. So for example, I would have a spread like this (for example):

(1 indicates a mild symptom, 5 indicates a fatality)
1 - 40%
2 - 25%
3 - 20%
4 - 14%
5 - 1%

I was playing around with generating numbers and using something like 6-(Power(randomNum,CAST(1/6) AS DECIMAL())
and then using FLOOR or CEILING to round up or down as necessary. The catch is that I'm trying to avoid using a cursor for all of the data generation. I may have to for the patients, and then loop until the code generates a grade 5, because that indicates a fatality. At that point, I would "deactivate" the patient so no more cycles would be run.

Is this a sane way to do this, or am I missing something obvious? (Sorry, I would read Ken Henderson's advice in Guru's Guide, but it's at home and I'm not.)

Thanks!
Pieter

Post #1471907
Posted Tuesday, July 9, 2013 8:06 PM


SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 2:57 PM
Points: 4,321, Visits: 6,115
How about something like this? You can do a table of numbers to generate a set of them with the required distribution.

DECLARE @i float = RAND(CHECKSUM(NEWID()))

SELECT CASE WHEN @i <= 0.4 THEN 1
WHEN @i > 0.4 AND @i <= 0.65 THEN 2
WHEN @i > 0.65 AND @i <= 0.85 THEN 3
WHEN @i > 0.85 AND @i <= 0.99 THEN 4
WHEN @i > 0.99 THEN 5
END


Best,

Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru at GMail
Post #1471919
Posted Tuesday, July 9, 2013 8:23 PM
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Today @ 12:47 PM
Points: 710, Visits: 4,541
I think that will work. I knew I had to be making it waaay harder than it really was.

Thanks!
Post #1471924
Posted Tuesday, July 9, 2013 8:44 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 4:53 PM
Points: 36,795, Visits: 31,257
Please post the link that you mentioned so others can see what you have seen. Thanks.

If you want to generate a million rows of sample data using those ratios, you could do the following. This takes about 1-1/2 seconds to gen a table with a million rows in it.

WITH
cteGenMillionNumbers AS
(
SELECT TOP 1000000
N = ABS(CHECKSUM(NEWID()))%100+1
FROM sys.all_columns ac1
CROSS JOIN sys.all_columns ac2
)
SELECT SomeID= IDENTITY(INT,1,1),
Grade = CASE
WHEN N BETWEEN 1 AND 40 THEN 1
WHEN N BETWEEN 41 AND 65 THEN 2
WHEN N BETWEEN 66 AND 85 THEN 3
WHEN N BETWEEN 86 AND 99 THEN 4
WHEN N = 100 THEN 5
ELSE 0 --will never happen but I do these types of safety checks
END
INTO #MyHead
FROM cteGenMillionNumbers
;



--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1471929
Posted Wednesday, July 10, 2013 1:29 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 5:45 PM
Points: 3,617, Visits: 5,237
Jeff's way is probably faster, but I have a general purpose function called RN_MULTINOMIAL that you can use for this.

See the 2nd article linked in my signature.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1471985
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse