Get the App
SLTechnology News&Howtos  ›  Database  › 

How to generate Random numbers with fixed digits in Mysql

Shulou Source: shulou.com Published: 2022-05-31 19:25:21 11月11日 Update

This article will explain in detail how to generate fixed-digit random numbers in Mysql. The content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

The project needs to generate some fixed-digit random numbers dynamically and randomly, such as 8-bit, 5-bit and so on.

The way I saw it before is like this.

ROUND (ROUND (RAND (), 5) * 100000)

The Rand () function is a random function with 0 ~ 1 (infinitely close).

If a random number takes out 0.05321

So what comes out in this way is 5321, only four.

If you can wrap it with a function and find that the number of digits is wrong after taking the value, it will be more perfect to fill it in.

The following is a function I modified, but the disadvantage is that the number of bits generated cannot exceed 20 digits. Of course, it's okay to change it.

DELIMITER $$

USE `prvecard` $$

DROP FUNCTION IF EXISTS `getRand` $$

CREATE `FUNCTION `getRand` (counts INTEGER) RETURNS VARCHAR (20) CHARSET utf8

BEGIN

DECLARE sTemp VARCHAR (20)

DECLARE sTempCounts INTEGER

SET sTemp = CONCAT (ROUND (ROUND (RAND (), counts) * (POW (10 points),)

IF (CHAR_LENGTH (sTemp))

Tags: Digits functions random numbers generation content articles more knowledge articles no no. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Linux Shulou Information OPPO Reno Redmi