Generate 6 Digit Random Number

How to generate 6 digit random number

void main() {
var rnd = new math.Random();
var next = rnd.nextDouble() * 1000000;
while (next < 100000) {
next *= 10;
}
print(next.toInt());
}

how can I generate a random 6 digits number with this rule? BY PHP

It seems you want to reject numbers that have:

  • a digit repeating for more than 3 times without interruption at either end of the number, OR:
  • a zero repeating for more than 3 times without interruption anywhere in the number.

This you can do with the following regular expression in a preg_match function call:

^(\d)\1\1\1|(\d)\2\2\2$|0000

If that matches, then you have a number that should be rejected.

Here is code to test several numbers:

$tests = array(
"000024", "241111", "222225", "143333", "500005", "999999",
"000124", "245111", "222555", "145333", "544445", "799997"
);

foreach($tests as $num) {
$reject = preg_match("~^(\d)\\1\\1\\1|(\d)\\2\\2\\2$|0000~", $num);
echo "$num: " . ($reject ? "Not OK" : "OK") . "\n";
}

The first 6 will print as "Not OK", the other 6 as "OK".

Your rndgen function could use that as follows:

function rndgen() {
do {
$num = sprintf('%06d', mt_rand(100, 999989));
} while (preg_match("~^(\d)\\1\\1\\1|(\d)\\2\\2\\2$|0000~", $num));
return $num;
}

DART: How do I generate a random six (6) digit number in flutter on a button click to display on the next page?

You can use the Random class

          Container(
width: MediaQuery.of(context).size.width * 3.5/4,
height: MediaQuery.of(context).size.height * 0.35/4,
child: RaisedButton(
color: Colors.indigoAccent,
textColor: cc.WHITE,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
onPressed: () {
int min = 100000; //min and max values act as your 6 digit range
int max = 999999;
var randomizer = new Random();
var rNum = min + randomizer.nextInt(max - min);
push(context, Invite(randomNum: rNum)); //pass your random number through
},
child: new Text(
'GENERATE CODE',
style: new TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.w700,
color: Colors.white,
),
),
//text: 'GENERATE CODE',
),
),

In your class

class Invite extends StatefulWidget{
Invite(Key key, this.randomNumber):super(key:key);
final int randomNumber;

....//your code

//to retrieve the number that has been sent use widget.randomNumber
}

Generate 6 digit random number in mysql

If the problem is that you are missing leading zeros, you can left pad with spaces:

UPDATE member
SET updates = LPAD(FLOOR(RAND() * 999999.99), 6, '0');

I hope you understand that "random" means "random" and different rows can get the same value.

How can I generate a 6 digit unique number?

$six_digit_random_number = random_int(100000, 999999);

As all numbers between 100,000 and 999,999 are six digits, of course.

Generate random 6 digit number

If you want a string to lead with zeroes, try this. You cannot get an int like 001.

    Random generator = new Random();
String r = generator.Next(0, 1000000).ToString("D6");

Generate random number of 6 digit length with at-least n unique digits in php

Asker Comment: 4 unique and 2 repetitive digits. But they should also have 3 unique
and 3 repetitive digits, 2 unique and 4 repetitive digits in random
number.

Ok, I think I finally understand, so I have tweaked the algorithm to match every possible rule you're expecting from 0 to 6 unique, though as you will see ive not changed much to my original answers code. Ive even added a sort flag so as to sort the int's incase that is also a requirement. Im determined!!

<?php
//
function random_number_with_dupe($len = 6, $dup = 1, $sort = false) {
if ($dup < 1) {
throw new InvalidArgumentException('Second argument is < 1');
}

$num = range(0,9);
shuffle($num);

$num = array_slice($num, 0, ($len-$dup)+1);

if ($dup > 0) {
$k = array_rand($num, 1);
for ($i=0; $i<($dup-1); $i++) {
$num[] = $num[$k];
}
}

if ($sort) {
sort($num);
}

return implode('', $num);
}

All unique.

for ($i=0; $i<5; $i++) {
echo random_number_with_dupe(6, 1, true).PHP_EOL;
}
/*
124579
123679
013568
015789
013578
*/

4 unique and 2 repetitive digits.

for ($i=0; $i<5; $i++) {
echo random_number_with_dupe(6, 2, true).PHP_EOL;
}
/*
235699
037789
034677
012249
033569
*/

3 unique and 3 repetitive digits.

for ($i=0; $i<5; $i++) {
echo random_number_with_dupe(6, 3, true).PHP_EOL;
}
/*
015559
011147
111239
677789
456777
*/

2 unique and 4 repetitive digits.

for ($i=0; $i<5; $i++) {
echo random_number_with_dupe(6, 4, true).PHP_EOL;
}
/*
068888
022229
018888
333378
000058
*/

1 unique and 5 repetitive digits.

for ($i=0; $i<5; $i++) {
echo random_number_with_dupe(6, 5, true).PHP_EOL;
}
/*
788888
066666
799999
355555
244444
*/

0 unique and 6 repetitive digits.

for ($i=0; $i<5; $i++) {
echo random_number_with_dupe(6, 6, true).PHP_EOL;
}
/*
888888
333333
777777
888888
777777
*/

All rules.

for ($i=0; $i<6; $i++) {
echo random_number_with_dupe(6, $i+1, true).PHP_EOL;
}
/*
345789
003569
245666
000089
777778
555555
*/

All rules (random).

for ($i=0; $i<6; $i++) {
echo random_number_with_dupe(6, mt_rand(1, 6), true).PHP_EOL;
}
/*
225678
222222
111359
444444
777778
233349
*/

Working example:

https://3v4l.org/3OCkc

Original

How about this, every number is unique. Use 0 to 9 for the entropy and then shuffle.

<?php
function random_number($len = 6) {
$num = range(0,9);
shuffle($num);
return implode('', array_slice($num, 0, $len));
}

for ($i=0; $i<10; $i++) {
echo random_number(6).PHP_EOL;
}

Result:

357964
365870
392576
285196
278915
712960
751032
517420
943257
380162

Edit

Asker Comment: This I can do but the requirement is different. They need
at-least 2 unique numbers. with 1 duplicate.

<?php
function random_number_with_1_dupe($len = 6) {
$num = range(0,9);
shuffle($num);

$num = array_slice($num, 0, $len-1);

$dup = array_rand($num, 1);
$num[] = $num[$dup];

return implode('', $num);
}

for ($i=0; $i<10; $i++) {
echo random_number_with_1_dupe(6).PHP_EOL;
}

564795
698756
937414
760897
706383
784626
520166
614055
798057
295809

Edit v4.0

I need a random number without these two rules : 1 unique and 5
repetitive digits. 0 unique and 6 repetitive digits. Rules applicable
: All unique. 4 unique and 2 repetitive digits. 3 unique and 3
repetitive digits. 2 unique and 4 repetitive digits.

The second argument is the rule, so you would to do:

echo random_number_with_dupe(6, mt_rand(1, 4), true).PHP_EOL;

If you want something inbetween you would define a rules array and pick out a random one.

$rules = [
1,3,5
];

echo random_number_with_dupe(6, $rules[array_rand($rules)], true);

Generate random 6-digit ID in Python

This oneliner will do the trick:

import random

random_id = ' '.join([str(random.randint(0, 999)).zfill(3) for _ in range(2)])

But to achieve the uniqueness you need to know the details about how it may be checked for that. For example, if you're writing it to the database using raw SQL, you will need to check for uniqueness: for example, SELECT id FROM my_table WHERE id=%s, where %s is your generated id. If it already exists, you need to generate it again. Basically you do something like this:

while True:
uid = ' '.join([str(random.randint(0, 999)).zfill(3) for _ in range(2)])
if uid_exists(uid): # Your function to check if it already exist
continue
write_your_record(uid) # Do something next if not

P.S.: You will also need to guarantee uniqueness in terms of atomic operations. I. e. you could face a problem if other application/worker/etc. writes the same ID to the database in that moment when you've already checked that ID doesn't exist in the DB, but didn't write your record yet. You can do that in some different ways:

1)

If your ID column in the database is constrained as UNIQUE, you can rely on that:

while True:
uid = ' '.join([str(random.randint(0, 999)).zfill(3) for _ in range(2)])
try:
write_your_record(uid)
break
except YourDatabaseAlreadyExistsError:
continue

You can do all these actions inside a transaction, that locks the database from writing in parallel.



Related Topics



Leave a reply



Submit