Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
How to generate 1000 random numbers ranging from 0.01 to 3.0 whose total is exactly 2000 in sql server 2008.

Please reply me.

[edit]SHOUTING removed, spurious bold, removed, urgency removed - OriginalGriff[/edit]
Posted
Updated 14-Aug-13 23:07pm
v3
Comments
Stephen Hewison 15-Aug-13 4:57am    
That's not possible. You max value is 0.3. Mutiply that by 1000 and you get a max total of 300. So your problem as you've described it is impossible to solve.
Ankit_shah1592 15-Aug-13 4:59am    
sorry its beetween 0.01 to 0.3.
please tell me.
Ankit_shah1592 15-Aug-13 4:59am    
sorry its beetween 0.01 to 3.0 please tell me.
Ankit_shah1592 15-Aug-13 5:00am    
i update my question. now please reply me.
OriginalGriff 15-Aug-13 5:08am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.
Urgency deleted: It may be urgent to you, but it isn't to us. All that your stressing the urgency does is to make us think you have left it too late, and want us to do it for you. This annoys some people, and can slow a response.

You're lucky I'm procrastinating.

What I've done is generated 1999 random numbers.

Performed a weight adjustment to bring the total of the numbers to within a tolerance of 1 number from the desired total without exceeding the specified min and max bounds.

I've then calculated the missing amount required to reach the target value and added this as the last number.

SQL
DECLARE
	@Min NUMERIC(10,2),
	@Max NUMERIC(10,2),
	@NumberQty INT,
	@TargetValue NUMERIC(10,2)
	
SELECT
	@Min = 0.01,
	@Max = 3,
	@NumberQty = 1000,
	@TargetValue = 2000
	

DECLARE @Count INT
SET @Count = 0

WHILE(@Count < @NumberQty - 1)
BEGIN

	IF @Count = 0
		BEGIN
			SELECT RndNum = CAST(((RAND() * (@Max - @Min)) + @Min) AS NUMERIC(3, 2)) INTO #Temp
		END
	ELSE
		BEGIN
			INSERT INTO #Temp SELECT RndNum = CAST(((RAND() * (@Max - @Min)) + @Min) AS NUMERIC(3, 2))
		END

	
	SET @Count = @Count + 1

END

DECLARE 
	@Total NUMERIC(10, 2), 
	@Missing NUMERIC(10, 2), 
	@Space NUMERIC(10, 2), 
	@MissAsPercOfSpace NUMERIC(20, 15), 
	@Adjusted NUMERIC(10, 2),
	@MidPoint NUMERIC(10, 2)

SET @MidPoint = @Min + ((@Max - @Min) / 2)

SELECT @Total = Sum(RndNum) FROM #Temp



SELECT @Missing = (@TargetValue - @MidPoint) - @Total

SELECT @Space = ((@Max * @NumberQty) - @MidPoint) - @Total

SELECT @MissAsPercOfSpace = @Missing / @Space

SELECT
	RndNum = CAST((RndNum + ((@Max - RndNum) * @MissAsPercOfSpace)) As NUMERIC(10,2))
INTO
	#Temp2
FROM
	#Temp
	
SELECT
	@Adjusted = Sum(RndNum)
FROM 
	#Temp2
	
INSERT INTO #Temp2
SELECT
	@TargetValue - @Adjusted

SELECT
	Total = Sum(RndNum), MinVal = Min(RndNum), MaxVal = Max(RndNum)
FROM 
	#Temp2


DROP TABLE #Temp
DROP TABLE #Temp2
 
Share this answer
 
Comments
Ankit_shah1592 15-Aug-13 5:42am    
But how i can see this 1000 row in a table.
Stephen Hewison 15-Aug-13 6:36am    
select * from #temp 2
Ankit_shah1592 15-Aug-13 5:47am    
Total is not 2000, its showing 1480.
Stephen Hewison 15-Aug-13 6:37am    
The @total variable is the total of the original random numbers. To get the total of the adjusted numbers, look at the last select statement.
Ankit_shah1592 15-Aug-13 5:50am    
Thanks you so much. Its 100% working... :) thanks boss.
You can't.

Random numbers are just that: random. In order to have a absolute value total, you would have to fix at least one value, probably more.
"Random" means just that: there is no relationship between and of the numbers which allows you to determine what the next one will be. It is entirely feasible that all 1000 random values would we the minimum: 0.01 or the maximum: 3.0

Unlikely, but entirely feasible.
 
Share this answer
 
Comments
Ankit_shah1592 15-Aug-13 5:18am    
Okay, then give me the solution that how i generate 1000 random numbers whose total is 2000. Forget that range of 0.01 to 3.0
OriginalGriff 15-Aug-13 5:21am    
What part of:
"You can't.

Random numbers are just that: random. In order to have a absolute value total, you would have to fix at least one value, probably more."
Did you not understand?
Ankit_shah1592 15-Aug-13 5:41am    
But how i can see this 1000 row in a table.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900