SQLUSA

Microsoft SQL Server 2000 Best Practices

 

How to simulate a casino roulette?

 

The following stored procedure will simulate a casino roulette wheel:

use pubs

create proc RouletteSimulator @Bet int, @Amount smallmoney
as
begin
declare @Result table (Message char(30), Wheel int, Win smallmoney)
declare @Wheel int

if ( @Bet < 1 or @Bet > 36 )
begin
insert @Result select 'Bet 1 through 36 only', -1, 0
select * from @Result
return
end

set @Wheel = ROUND(RAND(CAST(NEWID() AS VARBINARY))*38,0,-1)

if (@Wheel = @Bet)
begin
insert @Result select 'Lucky! You won: ', @Bet, 36 * @Amount
select * from @Result
return
end
insert @Result select 'Sorry, you lost', @Wheel, -1.0 * @Amount

select * from @Result

end

-- exec RouletteSimulator 10, 100.0

 

 

The World Leader in SQL Server Training
 
SQLUSA.com Home Page