Skip to content

Instantly share code, notes, and snippets.

@sfaut
Created March 11, 2023 16:51
Show Gist options
  • Save sfaut/7e13efb44b1a4eca984764027200dd82 to your computer and use it in GitHub Desktop.
Save sfaut/7e13efb44b1a4eca984764027200dd82 to your computer and use it in GitHub Desktop.
Get a random integer between start and stop excluded
-- Get a random integer between start and stop excluded
-- Author : sfaut <https://github.com/sfaut>
-- Publication date : 2023-03-11
-- Tested with MySQL 8.0.32 (@@sql_mode='ANSI,TRADITIONAL')
CREATE FUNCTION RANDOM_INTEGER(start INT, stop INT)
RETURNS INT
NOT DETERMINISTIC
NO SQL
COMMENT 'Get a random integer between start and stop excluded'
RETURN FLOOR(start + RAND() * (stop - start));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment