Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fill table with random values

Status
Not open for further replies.

RobV

Programmer
Feb 8, 2001
66
NL
I'm trying to fill a database with some testdata:

update mgMainScore set Score = (rand() * 5) + 1

But all the rows in the table have the same value. Any help?

Thnx,
Rob.
 
Hi Rob

The reason the random number is not changing is you have to change the seed(starting value) that the random function uses to generate the random number. The random number is not truly random as it is based on a seed.

This example uses a seed (@counter) and generates 5 random numbers, each time through the loop it adds 1 to @counter thus changing the seed.


DECLARE @counter smallint
SET @counter = 1
WHILE @counter < 5
BEGIN
SELECT RAND(@counter)
SET @counter = @counter + 1 -- CHANGING THE SEED VALUE
END
GO


Hope this Helps
Bernadette X-)

 
I getter better (more random) results if the seed is removed, in the above example
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top