Function/proceedure to Generate mysql number between 2 ranges

For example,
select gennum(0,1);
returns either 0,1

This is to be used as follows:
select spaces(gennum(0,1));

Obviously the result has to be in whole numbers as the spaces function will only accept whole numbers.
divinemrAsked:
Who is Participating?
 
Dave BaldwinFixer of ProblemsCommented:
From this page http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_rand :

To obtain a random integer R in the range i <= R < j, use the expression FLOOR(i + RAND() * (j – i)). For example, to obtain a random integer in the range the range 7 <= R < 12, you could use the following statement:

SELECT FLOOR(7 + (RAND() * 5));


Between 0 and 5 for you...
select space(FLOOR(RAND() * 5));

Open in new window

0
 
divinemrAuthor Commented:
Can you please make it a bit more like:

select spaces(0,3) # Generates either no spaces, 1, 2 or 3 spaces
0
 
divinemrAuthor Commented:
Nevermind, I got it .. thanks
0
 
Dave BaldwinFixer of ProblemsCommented:
You're welcome.
0
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.

All Courses

From novice to tech pro — start learning today.