Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
shapper
Constraint Violating Yak Guru
450 Posts |
Posted - 2008-02-24 : 17:38:18
|
| Hello,How to get a random bit and a random int?Thanks,Miguel |
|
|
tprupsis
Yak Posting Veteran
88 Posts |
Posted - 2008-02-24 : 18:05:13
|
| You can use the rand() function to do this. Rand() returns a float between 0 and 1 so multiplying by a factor and then converting to int should work to return a random int: convert(int, 1000*rand())As far as returning a bit, just use 2 as your factor. |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2008-02-24 : 18:05:16
|
| This will work for random intDECLARE @Random int;DECLARE @Upper int;DECLARE @Lower int-- This will create a random number between 1 and 999SET @Lower = 1 -- The lowest random numberSET @Upper = 999 -- The highest random numberSELECT @Random = Round(((@Upper - @Lower -1) * Rand() + @Lower), 0)SELECT @Random |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-02-25 : 03:46:41
|
quote: Originally posted by sodeep This will work for random intDECLARE @Random int;DECLARE @Upper int;DECLARE @Lower int-- This will create a random number between 1 and 999SET @Lower = 1 -- The lowest random numberSET @Upper = 999 -- The highest random numberSELECT @Random = Round(((@Upper - @Lower -1) * Rand() + @Lower), 0)SELECT @Random
orselect abs(checksum(newid()))%1000MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|