How do you generate a non repeating random number in php?

  1. Home
  2. Mysql
  3. How To Generate Random Number Without Repeat In Database Using Php

How do you generate a non repeating random number in php?
How do you generate a non repeating random number in php?
How do you generate a non repeating random number in php?
How do you generate a non repeating random number in php?
How do you generate a non repeating random number in php?
How do you generate a non repeating random number in php?
How do you generate a non repeating random number in php?
How do you generate a non repeating random number in php?

How to Generate Random number without repeat in database using PHP?

Tags: mysql Answers: 1 | Viewed 66,388 times

I would like to generate a 5 digit number which do not repeat inside the database.
Say I have a table named numbers_mst with field named my_number.

I want to generate the number the way that it do not repeat in this my_number field.
And preceding zeros are allowed in this. So numbers like 00001 are allowed.
Another thing is it should be between 00001 to 99999. How can I do that?

One thing I can guess here is I may have to create a recursive function to check number into table and generate.



How do you generate a non repeating random number in php?
Tushar answer at 2012-07-27 56



SELECT FLOOR(RAND() * 99999) AS random_num
FROM numbers_mst
WHERE "random_num" NOT IN (SELECT my_number FROM numbers_mst)
LIMIT 1

What this does:



  1. Selects random number between 0 - 1 using RAND().

  2. Amplifies that to be a number between 0 - 99999.

  3. Only chooses those that do not already exist in table.

  4. Returns only 1 result.


* The answers/resolutions are collected from stackoverflow, are licensed under CC BY-SA 3.0

Some Code Answers


SELECT FLOOR(RAND() * 99999) AS random_num FROM numbers_mst  WHERE "random_num" NOT IN (SELECT my_number FROM numbers_mst) LIMIT 1 

SELECT random_num FROM (   SELECT FLOOR(RAND() * 99999) AS random_num    UNION   SELECT FLOOR(RAND() * 99999) AS random_num ) AS numbers_mst_plus_1 WHERE `random_num` NOT IN (SELECT my_number FROM numbers_mst) LIMIT 1 

SELECT random_num FROM (   SELECT FLOOR(RAND() * 99999) AS random_num ) AS numbers_mst_plus_1 WHERE random_num NOT IN (SELECT my_number FROM numbers_mst WHERE my_number IS NOT NULL) LIMIT 1 

function unique_code_generator($prefix='',$post_fix='')
{
$t=time();
return ( rand(000,111).$prefix.$t.$post_fix);
}

SELECT LPAD(FLOOR(RAND()*99999),5,0)

SELECT random_num FROM (
select a.a + (10 * b.a) + (100 * c.a) + (1000 * d.a) + (10000 * e.a) as random_num
from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as d
cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as e ) q WHERE random_num NOT IN(SELECT my_number FROM numbers_mst) ORDER BY RAND() LIMIT 1

DELIMITER $$  USE `temp` $$  DROP PROCEDURE IF EXISTS `GenerateUniqueValue`$$  CREATE PROCEDURE `GenerateUniqueValue`(IN tableName VARCHAR(255),IN columnName VARCHAR(255))  BEGIN
DECLARE uniqueValue VARCHAR(8) DEFAULT "";
DECLARE newUniqueValue VARCHAR(8) DEFAULT "";
WHILE LENGTH(uniqueValue) = 0 DO
SELECT CONCAT(SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789', RAND()*34+1, 1),
SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789', RAND()*34+1, 1),
SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789', RAND()*34+1, 1),
SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789', RAND()*34+1, 1),
SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789', RAND()*34+1, 1),
SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789', RAND()*34+1, 1),
SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789', RAND()*34+1, 1),
SUBSTRING('ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789', RAND()*34+1, 1)
) INTO @newUniqueValue;
SET @rcount = -1;
SET @query=CONCAT('SELECT COUNT(*) INTO @rcount FROM ',tableName,' WHERE ',columnName,' like ''',newUniqueValue,'''');
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
IF @rcount = 0 THEN SET uniqueValue = @newUniqueValue ;
END IF ;
END WHILE ;
SELECT uniqueValue;
END$$ DELIMITER ;

call GenerateUniqueValue('tablename', 'columnName') 

$regenerateNumber = true;
do {
$regNum
= rand(2200000, 2299999);
$checkRegNum = "SELECT * FROM teachers WHERE teacherRegNum = '$regNum'";
$result
= mysqli_query($connection, $checkRegNum);
if (mysqli_num_rows($result) == 0) {
$regenerateNumber = false;
} } while ($regenerateNumber);

CREATE DEFINER=`pro`@`%` PROCEDURE `get_rand`() BEGIN DECLARE regenerateNumber BOOLEAN default true;
declare regNum int;
declare cn varchar(255);
repeat SET regNum
:= FLOOR(RAND()*90000000+10000000);
SET cn =(SELECT count(*) FROM stock WHERE id = regNum);
select regNum;
if cn=0 then SET regenerateNumber = false;
end if;
UNTIL regenerateNumber=false end repeat;
END

PHP rand() Function - W3Schools

5 days ago Definition and Usage The rand () function generates a random integer. Example tip: If you want a random integer between 10 and 100 (inclusive), use rand (10,100). Tip: As of PHP 7.1, the rand () function has been an alias of the mt_rand () function.Syntax rand (); or rand ( min,max );Parameter ValuesTechnical Details ❮ PHP Math Reference

Show details

See also: Function

How to Generate Random number without repeat in …

2 days ago Jul 27, 2012  · SELECT FLOOR (RAND () * 99999) AS random_num FROM numbers_mst WHERE "random_num" NOT IN (SELECT my_number FROM numbers_mst) LIMIT 1. …

Show details

php - Generating random numbers without repeats

4 days ago Jul 21, 2013  · Generate a random listing ID using the method given above, and then store it your database table. On each page refresh, generate a random listing ID, and check if it …

Reviews: 4

Show details

See also: Php Database List

How to Generate Random number without repeat in …

5 days ago How to Generate Random number without repeat in database using PHP - MySQL [ Ext for Developers : https://www.hows.tech/p/recommended.html ] How to Generate...

Show details

See also: Html Database

MySQL : How to Generate Random number without …

2 days ago MySQL : How to Generate Random number without repeat in database using PHP? [ Beautify Your Computer : https://www.hows.tech/p/recommended.html ] MySQL : Ho...

Show details

See also: Html Database

How to generate a unique random number in PHP?

5 days ago How to generate random number without repeat in database? In addition to Tushar’s answer to make it work when numbers_mst is empty: The first, suggested in other answer, is to …

Show details

See also: Database

How do you generate random numbers without repeats?

1 week ago Which is used to generate a random integer number. Syntax. The basic syntax of rand() function is following: rand(); or rand(min,max); How to generate random number without repeat in …

Show details

See also: Function

How To Generate Random Numbers In PHP - pTutorial

1 week ago Explanation. rand () function is a PHP built-in function that generates a unique number. If you want to generate a unique number within a range, then define the start and end point of …

Show details

See also: Function

Generating Random Number and inserting into database

2 days ago May 30, 2011  · I need a way to generate a random number and insert into a database, and I need the database to not contain any duplicates of that number. I basically need to generate

Show details

See also: Database

PHP rand() Function - W3Schools

1 week ago Definition and Usage. The rand () function generates a random integer. Example tip: If you want a random integer between 10 and 100 (inclusive), use rand (10,100). Tip: As of PHP 7.1, the …

Show details

See also: Function

php - How to generate random numbers starting from 00000 to …

5 days ago Good morning Bruno. I think you can use this here: select FLOOR(RAND() * 99999) Or rather: BEGIN DECLARE valorRand bigint; set valorRand = select FLOOR(RAND() * 99999); if exists …

Show details

See also: Php

How to show the random number from database without …

2 days ago May 19, 2015  · Shuffle again for the next sequence of records. [Edit: MTH] First get an array of the record objects (or just the IDs). Call it orderedData. Call var shuffledData = Shuffle …

Show details

See also: Database

generate random numbers in php with no repeats · GitHub - Gist

2 days ago Mar 15, 2018  · random number generation, no repeats This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open …

Show details

See also: Php File

Please leave your answer here:

How do you generate a random number without repeating it?

Generate Random Number List With No Duplicates in Excel.
Select cell B3 and click on it..
Insert the formula: =RANDBETWEEN(10,30).
Press enter..
Drag the formula down to the other cells in the column by clicking and dragging the little “+” icon at the bottom-right of the cell..

How do I generate a random number in PHP?

The rand() function generates a random integer. Example tip: If you want a random integer between 10 and 100 (inclusive), use rand (10,100). Tip: As of PHP 7.1, the rand() function has been an alias of the mt_rand() function.

How can I generate 5 random numbers in PHP?

rand() or mt_rand() functions can be used to Generate 5 Digit Random Number in PHP.

How do I generate a random 4 digit number in PHP?

rand() or mt_rand() functions can be used to Generate 4 Digit Random Number in PHP.