How to generate random numbers with specified digits in MySQL and how to generate random numbers in batches

How to generate random numbers with specified digits in MySQL and how to generate random numbers in batches

1. First introduce several commonly used MySQL functions

RAND() randomly generates a decimal between 0 and 1 (0<1)

CEILING Round up
FLOOR Round down

2. Generate random numbers

-- Generate a 3-digit random number SELECT CEILING(RAND()*900+100);
-- Generate a 4-digit random number SELECT CEILING(RAND()*9000+1000);
-- Generate a 5-digit random number SELECT CEILING(RAND()*90000+10000);
...

Example:

To explain:

-- Generate a random decimal between 0 and 1 0 < RAND() < 1

-- Multiply by 9000 to get a random decimal between 0 and 9000 0 < RAND*9000 < 9000

-- Add 1000 to get a random decimal between 0 and 10000 0 < RAND*9000+1000 < 10000

-- Use the CEILING function to round up, remove the trailing decimals, and get an integer -- or use the FLOOR function to round down, both are CEILING(RAND()*9000+1000)
-- or FLOOR(RAND()*9000+1000)

3. Note

This method of generating a specified number of random numbers is not particularly good because there is a possibility of duplication. The fewer digits a random number has, the greater the probability of repetition. Therefore, this method is only useful in special occasions.

mysql batch generate random numbers

Fake data:

update exercise_data set star_num=FLOOR(1 + (RAND() * 5));

The rand function randomly generates a random number between 0 and 1. Multiplying by 5 will get a random number between 1 and 5, and floor is the previous integer.

The above is the method that I introduced to you to generate a random number of specified digits in MySQL. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • Tips for using MySQL's specified range random number function rand()
  • mysql insert a random number into a field (insert a random number into the MySQL database)
  • MySQL Query Random Data 4 Methods and Performance Comparison
  • How to generate random numbers, strings, dates, verification codes and UUIDs in Oracle
  • MySQL method of generating random numbers, strings, dates, verification codes and UUIDs

<<:  Implementation of installing Docker in win10 environment

>>:  Vue mobile terminal realizes the whole process of left sliding editing and deletion

Recommend

Analysis of two implementation methods for adding static routing in Linux

Command to add a route: 1.Route add route add -ne...

How to test the maximum number of TCP connections in Linux

Preface There is a misunderstanding about the max...

Native JavaScript carousel implementation method

This article shares the implementation method of ...

Detailed steps to download Tomcat and put it on Linux

If you have just come into contact with Linux, th...

Vue project implements graphic verification code

This article example shares the specific code of ...

Install and configure MySQL 5.7 under CentOS 7

This article tests the environment: CentOS 7 64-b...

VM VirtualBox virtual machine mount shared folder

One environment Install VMware Tools on CentOS 7 ...

Web interview Vue custom components and calling methods

Import: Due to project requirements, we will enca...

Detailed explanation of Linux commands sort, uniq, tr tools

Sort Tool The Linux sort command is used to sort ...

MySQL 5.5.27 installation graphic tutorial

1. Installation of MYSQL 1. Open the downloaded M...

MYSQL transaction tutorial Yii2.0 merchant withdrawal function

Preface I am a PHP programmer who started out as ...

Implementation of iview permission management

Table of contents iview-admin2.0 built-in permiss...

How to install and connect Navicat in MySQL 8.0.20 and what to pay attention to

Things to note 1. First, you need to create a my....

When to use table and when to use CSS (experience sharing)

The main text page of TW used to have a width of 8...