MySQL: Select Random Number with One Exception

Using MySQL, how can I select a random number between 1 and 100 that is NOT 33?  

I use this to select a random number between 1 and 100:
SELECT FLOOR(1+RAND()*100);

Open in new window

However I never want "33" to be selected.
LVL 10
skijAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Root777Software JediCommented:
select CASE WHEN tbl.i > 32 THEN tbl.i + 1 ELSE tbl.i END as num from 
(SELECT FLOOR(1+RAND()*99) as i) as tbl

Open in new window

0
Randy PooleCommented:
Create a user defined function which will select a different random if 33 is encountered:
CREATE FUNCTION `rand_not33`() RETURNS int(11)
BEGIN
DECLARE ret int;
Set ret=33;
while ret=33 do
SELECT FLOOR(1+RAND()*100) INTO ret;
end while;

RETURN ret;

END

Open in new window


Then just call it as:

Select rand_not33()

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
skijAuthor Commented:
Thank you both.  I like the idea of a user defined function but unfortunately my shared host has disabled this functionality.  The idea posted by Root777 works.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.