Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Random Number Generator Rate Topic Display Mode Topic Options
Author
 Message
 Posted Tuesday, April 30, 2013 7:11 AM
 Valued Member Group: General Forum Members Last Login: Wednesday, January 15, 2014 1:56 PM Points: 59, Visits: 120
 I am trying to create a random number generator that would populate a number between 8-48 to 9 different lanes with the only criteria being that each lane number cannot be within 5 of any lane within 3 spaces of it. So what I mean is if lane 4 has a number of 30 then lane 1-2-3 and lane 5-6-7 cannot be within 5 of lane 4's number. Hope this makes sense and any help would be sincerely appreciated....
Post #1448009
 Posted Tuesday, April 30, 2013 7:57 AM
 SSCrazy Eights Group: General Forum Members Last Login: Thursday, December 1, 2016 10:11 AM Points: 8,587, Visits: 18,753
 todd.ayers (4/30/2013)I am trying to create a random number generator that would populate a number between 8-48 to 9 different lanes with the only criteria being that each lane number cannot be within 5 of any lane within 3 spaces of it. So what I mean is if lane 4 has a number of 30 then lane 1-2-3 and lane 5-6-7 cannot be within 5 of lane 4's number. Hope this makes sense and any help would be sincerely appreciated....No problem. Folks here love this sort of question! Here's a solution for openers:`;WITH SemiRandomNumbers AS ( SELECT Lane = 8 + ABS(CHECKSUM(NEWID()))%41 FROM syscolumns a)SELECT *FROM (SELECT ID = 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4) sCROSS APPLY ( SELECT TOP 1 Lane1 = Lane FROM SemiRandomNumbers --ORDER BY (SELECT NULL)) l1CROSS APPLY ( SELECT TOP 1 Lane2 = Lane FROM SemiRandomNumbers r WHERE NOT (r.Lane BETWEEN l1.Lane1-5 AND l1.Lane1+5)) l2CROSS APPLY ( SELECT TOP 1 Lane3 = Lane FROM SemiRandomNumbers r WHERE NOT (r.Lane BETWEEN l1.Lane1-5 AND l1.Lane1+5) AND NOT (r.Lane BETWEEN l2.Lane2-5 AND l2.Lane2+5)) l3CROSS APPLY ( SELECT TOP 1 Lane4 = Lane FROM SemiRandomNumbers r WHERE NOT (r.Lane BETWEEN l1.Lane1-5 AND l1.Lane1+5) AND NOT (r.Lane BETWEEN l2.Lane2-5 AND l2.Lane2+5) AND NOT (r.Lane BETWEEN l3.Lane3-5 AND l3.Lane3+5)) l4CROSS APPLY ( SELECT TOP 1 Lane5 = Lane FROM SemiRandomNumbers r WHERE NOT (r.Lane BETWEEN l2.Lane2-5 AND l2.Lane2+5) AND NOT (r.Lane BETWEEN l3.Lane3-5 AND l3.Lane3+5) AND NOT (r.Lane BETWEEN l4.Lane4-5 AND l4.Lane4+5)) l5CROSS APPLY ( SELECT TOP 1 Lane6 = Lane FROM SemiRandomNumbers r WHERE NOT (r.Lane BETWEEN l3.Lane3-5 AND l3.Lane3+5) AND NOT (r.Lane BETWEEN l4.Lane4-5 AND l4.Lane4+5) AND NOT (r.Lane BETWEEN l5.Lane5-5 AND l5.Lane5+5)) l6CROSS APPLY ( SELECT TOP 1 Lane7 = Lane FROM SemiRandomNumbers r WHERE NOT (r.Lane BETWEEN l4.Lane4-5 AND l4.Lane4+5) AND NOT (r.Lane BETWEEN l5.Lane5-5 AND l5.Lane5+5) AND NOT (r.Lane BETWEEN l6.Lane6-5 AND l6.Lane6+5)) l7CROSS APPLY ( SELECT TOP 1 Lane8 = Lane FROM SemiRandomNumbers r WHERE NOT (r.Lane BETWEEN l5.Lane5-5 AND l5.Lane5+5) AND NOT (r.Lane BETWEEN l6.Lane6-5 AND l6.Lane6+5) AND NOT (r.Lane BETWEEN l7.Lane7-5 AND l7.Lane7+5)) l8CROSS APPLY ( SELECT TOP 1 Lane9 = Lane FROM SemiRandomNumbers r WHERE NOT (r.Lane BETWEEN l6.Lane6-5 AND l6.Lane6+5) AND NOT (r.Lane BETWEEN l7.Lane7-5 AND l7.Lane7+5) AND NOT (r.Lane BETWEEN l8.Lane8-5 AND l8.Lane8+5)) l9` “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail ShawFor fast, accurate and documented assistance in answering your questions, please read this article.Understanding and using APPLY, (I) and (II) Paul White Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff ModenExploring Recursive CTEs by Example Dwain Camps
Post #1448043
 Posted Tuesday, April 30, 2013 8:23 AM
 Valued Member Group: General Forum Members Last Login: Wednesday, January 15, 2014 1:56 PM Points: 59, Visits: 120
 ok... so how can I test this to see if works? What part should I look at or copy?
Post #1448060
 Posted Tuesday, April 30, 2013 8:27 AM
 SSCrazy Eights Group: General Forum Members Last Login: Thursday, December 1, 2016 10:11 AM Points: 8,587, Visits: 18,753
 Copy the lot, paste it into an SSMS window and execute it. “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail ShawFor fast, accurate and documented assistance in answering your questions, please read this article.Understanding and using APPLY, (I) and (II) Paul White Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff ModenExploring Recursive CTEs by Example Dwain Camps
Post #1448063
 Posted Tuesday, April 30, 2013 8:41 AM
 Valued Member Group: General Forum Members Last Login: Wednesday, January 15, 2014 1:56 PM Points: 59, Visits: 120
 well..... Darn.. I dont have SQL on my desktop... only access 2007
Post #1448071
 Posted Tuesday, April 30, 2013 8:45 AM
 SSCrazy Eights Group: General Forum Members Last Login: Thursday, December 1, 2016 10:11 AM Points: 8,587, Visits: 18,753
 todd.ayers (4/30/2013)well..... Darn.. I dont have SQL on my desktop... only access 2007Sorry, I can only guarantee that it will run in SQL Server 2008 - this is, after all, the SQL Server 2008 forum section Access will probably want to join it to a database down the network somewhere and plug on a front end with user toys. Be firm. “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail ShawFor fast, accurate and documented assistance in answering your questions, please read this article.Understanding and using APPLY, (I) and (II) Paul White Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff ModenExploring Recursive CTEs by Example Dwain Camps
Post #1448076
 Posted Tuesday, April 30, 2013 9:17 AM
 Valued Member Group: General Forum Members Last Login: Wednesday, January 15, 2014 1:56 PM Points: 59, Visits: 120
 Thx Bro I really really appreciate all your help!!!!
Post #1448102
 Posted Tuesday, April 30, 2013 9:26 AM
 SSCrazy Eights Group: General Forum Members Last Login: Thursday, December 1, 2016 10:11 AM Points: 8,587, Visits: 18,753
 todd.ayers (4/30/2013)Thx Bro I really really appreciate all your help!!!!BRO?!!! Are you an old git too? “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail ShawFor fast, accurate and documented assistance in answering your questions, please read this article.Understanding and using APPLY, (I) and (II) Paul White Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff ModenExploring Recursive CTEs by Example Dwain Camps
Post #1448111
 Posted Tuesday, April 30, 2013 11:02 AM
 Valued Member Group: General Forum Members Last Login: Wednesday, January 15, 2014 1:56 PM Points: 59, Visits: 120
 LOL!! yeah sorry hope I didnt offend you?
Post #1448148
 Posted Wednesday, May 1, 2013 7:10 AM
 SSCrazy Eights Group: General Forum Members Last Login: Thursday, December 1, 2016 10:11 AM Points: 8,587, Visits: 18,753
 todd.ayers (4/30/2013)LOL!! yeah sorry hope I didnt offend you?Heck no! Have you tested the code yet? “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail ShawFor fast, accurate and documented assistance in answering your questions, please read this article.Understanding and using APPLY, (I) and (II) Paul White Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff ModenExploring Recursive CTEs by Example Dwain Camps
Post #1448406

 Permissions