This is not a new feature by any means but it is a question I
have happen to see pop up every now and then. So a quick example
is following.
To generate a random integer within MySQL you can use the Floor
and Rand functions. The MySQL manual documents this here:
http://dev.mysql.com/doc/refman/5.5/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))"
So to give an example:
> SET @I = 3; # lower range
> SET @J = 43 - @I; # max range minus lower range
> SELECT FLOOR( @I + (RAND() * @J )) as
lottery_numbers\G
…
The easiest way to generate random rows in MySQL is to use the ORDER BY RAND() clause. SELECT col1 FROM tbl ORDER BY RAND() LIMIT 10; This can work fine for small tables. However, for big table, it will have a serious performance problem as in order to generate the list of random rows, MySQL […]
Some days ago I was working in a vocabulary game and dictionary. The dictionary contains 1,10,000 words and meanings. I developed a vocabulary game where I had to randomly choose 10 words out of 1,10,000 dataset. Here I’m describing the possible solutions for this problem and which solution I used.
Data table
Table name is dictionary and it has id, word and meaning fields. id contains auto incremented id and it is unbroken sequence 1,2,3…..n.
id | word | meaning |
1 | aback | Having the wind against the forward side of the sails |
2 | abandon | Forsake, leave behind |
… | ….. | …. … |