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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Number Generator getting Asterisks 3

Status
Not open for further replies.

teblack

Programmer
Apr 30, 2004
45
Below is what I'm using to generate a random number based on a new input record. It works fine with the expecetion of once in a while it will generate an asterisks for the field, which is wrong. Can anyone tell me why it's doing that.

Code:
DECLARE @max int
DECLARE @sublocation char(3)
SET @max = 999
SET @sublocation = convert(int, rand()* 100000) % @max + 100
select @sublocation
[/copde]

I need the number to be a three digit number betweeen 001 - 999

Thanks in advance for the help with this.

Thanks again,


TBlack -
 
Because of +100 offset, this code generates numbers in range 100 - 1099. So when random number hits 4 digits, you get format overflow character instead (*).

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Try this...

SET @sublocation = convert(int, rand() * @Max) + 1



-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Thanks, both changes, fixed the problem...

TBlack -
 
In general, to generate a random number between @min and @max (inclusive) you can use:

Code:
SELECT CAST(RAND() * (@max - @min + 1) AS int) + @min

--James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top