Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

 SQL Server Forums Profile | Active Topics | Members | Search | Forum FAQ Register Now and get your question answered!
 All Forums  General SQL Server Forums  New to SQL Server Programming  Create random number Reply to Topic  Printer Friendly
Author  Topic

GaryNull
Starting Member

USA
14 Posts

 Posted - 12/27/2013 :  15:58:14 Is it possible to create random numberswithin a range like : .28 to .57 ? UPDATE Nop_ProductVariant SET percent = RAND() .28 to .57So the percent field would look like :ProdID percent1001 .381002 .291003 .40etc . . all rows having a different value Edited by - GaryNull on 12/27/2013 16:59:55

cgraus
Starting Member

Australia
12 Posts

 Posted - 12/27/2013 :  16:18:26 If you want a list of values in random order, with no repeats, this will do that:WITH E1(N) AS( SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1), --10E+1 or 10 rowsE2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rowsE4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows maxcteTally(N) AS( SELECT 0 UNION ALL SELECT TOP (1000) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4)select n/100.0 from cteTally where n >= 280 and n <= 570 order by newid()You can simplify it by selecting top 570 and only doing a >= check, or even this:WITH E1(N) AS( SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1), --10E+1 or 10 rowsE2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rowsE4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows maxcteTally(N) AS( SELECT 0 UNION ALL SELECT TOP (500) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4)select (n+270)/100.0 from cteTally order by newid() Edited by - cgraus on 12/27/2013 16:26:14

GaryNull
Starting Member

USA
14 Posts

 Posted - 12/27/2013 :  16:25:32 thanks cgraus,But is it possible to have a different random value for each row?All the rows in the table contain the same value when I run that.Is it possible to do this :ProdID percent1001 .351002 .27........

cgraus
Starting Member

Australia
12 Posts

 Posted - 12/27/2013 :  16:26:55 quote:Originally posted by GaryNullthanks cgraus,But is it possible to have a different random value for each row?All the rows in the table contain the same value when I run that.Is it possible to do this :ProdID percent1001 .351002 .27........What I did was completely wrong :-) I went back and changed it.

khtan
In (Som, Ni, Yak)

Singapore
17689 Posts

 Posted - 12/27/2013 :  22:29:50 `select dbo.F_RANDOM_INTEGER(28, 57, newid()) / 100.0`http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=69499KHTime is always against us

cgraus
Starting Member

Australia
12 Posts

 Posted - 12/27/2013 :  22:57:37 To be clear, the code you posted does not work without installing the functions behind it. Then, it might be a better solution than mine, if you want an endless stream of random values and don't care about duplicates.

GaryNull
Starting Member

USA
14 Posts

 Posted - 12/27/2013 :  23:34:32 I found this and it looks like it works.This creates random number between 27 and 50 and a different value for every row :SELECT table_name, 27.0 + floor(23 * RAND(convert(varbinary, newid())))as magic_number FROM information_schema.tablesorder by magic_number

cgraus
Starting Member

Australia
12 Posts

 Posted - 12/28/2013 :  00:34:16 I ran this and the magic number sequence was all between 27 and 50. The sequence was 28, 29, 29, 33, 33, 33, 34, 35.... It will NOT give you a different value for each row, nor is it converting the values to the floating point range you wanted. The only way to get all unique values ( which you seem now to want ), is the way I did it, where each value is accounted for once, and the order is what's random. If you generate each number randomly, there will be repeats.

SwePeso
Patron Saint of Lost Yaks

Sweden
30421 Posts

 Posted - 12/28/2013 :  05:34:24 quote:Originally posted by GaryNullIs it possible to create random numberswithin a range like : .28 to .57 ?Yes, but there are only 30 unique numbers within your wanted interval.Do you have more than 30 rows?Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA

SwePeso
Patron Saint of Lost Yaks

Sweden
30421 Posts

 Posted - 12/28/2013 :  05:38:21 ```UPDATE w SET w.[Percent] = d.[Percent] FROM ( SELECT TOP(30) [Percent], ROW_NUMBER() OVER (ORDER BY NEWID()) AS rn FROM dbo.Nop_ProductVariant ) AS w INNER JOIN ( SELECT Number / 100E AS [Percent], ROW_NUMBER() OVER (ORDER BY NEWID()) AS rn FROM master.dbo.spt_values WHERE Type = 'P' AND Number BETWEEN 28 AND 57 ) AS d ON d.rn = w.rn```Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA

waterduck
Aged Yak Warrior

Malaysia
982 Posts

 Posted - 12/29/2013 :  22:04:11 Random number between 0.28 -> 0.57``` SELECT 0.28 + ((ROUND(((CONVERT(DECIMAL(3,2), RAND()) % 1) * 100) / 3, 0)) / 100) - (ROUND(((CONVERT(DECIMAL(3,2), RAND()) % 1) * 100) / 25, 0) / 100) ``` Edited by - waterduck on 12/29/2013 22:16:50

waterduck
Aged Yak Warrior

Malaysia
982 Posts

 Posted - 12/29/2013 :  22:45:39 ``` UPDATE Nop_ProductVariant SET [percent] = 0.28 + (SELECT ((ROUND((RandumNumber.RandumNumber * 100) / 3, 0)) / 100) -(ROUND((RandumNumber.RandumNumber * 100) / 25, 0) / 100) FROM (SELECT (ABS(CHECKSUM(NEWID())) % 100) / 100.0 AS RandumNumber)RandumNumber) ```

waterduck
Aged Yak Warrior

Malaysia
982 Posts

 Posted - 12/29/2013 :  22:48:13 XD finally can win peso 1 query mua ha ha ha
Topic
 Reply to Topic  Printer Friendly Jump To: Select Forum General SQL Server Forums       New to SQL Server Programming       New to SQL Server Administration       Script Library       Data Corruption Issues       Database Design and Application Architecture SQL Server 2012 Forums       Transact-SQL (2012)       SQL Server Administration (2012)       SSIS and Import/Export (2012)       Analysis Server and Reporting Services (2012)       Replication (2012)       Availability Groups and DR (2012)       Other SQL Server 2012 Topics SQL Server 2008 Forums       Transact-SQL (2008)       SQL Server Administration (2008)       SSIS and Import/Export (2008)       High Availability (2008)       Replication (2008)       Analysis Server and Reporting Services (2008)       Other SQL Server 2008 Topics SQL Server 2005 Forums       Transact-SQL (2005)       SQL Server Administration (2005)       .NET Inside SQL Server (2005)       SSIS and Import/Export (2005)       Service Broker (2005)       Replication (2005)       High Availability (2005)       Analysis Server and Reporting Services (2005)       Express Edition and Compact Edition (2005)       Other SQL Server Topics (2005) SQL Server 2000 Forums       SQL Server Development (2000)       SQL Server Administration (2000)       Import/Export (DTS) and Replication (2000)       Transact-SQL (2000)       Analysis Services (2000)       MSDE (2000) Development Tools       ASP.NET       Reporting Services Development       Other Development Tools Site Related Forums       Site Related Discussions       Article Discussion       Poll Discussion       The Yak Corral Other Forums       SQL Server 6.5 \ SQL Server 7.0       Other Topics       MS Access       ClearTrace Support Forum Old Forums       CLOSED - General SQL Server       CLOSED - SQL Server 2005/Yukon  -------------------- Home Active Topics Frequently Asked Questions Member Information Search Page
 SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC