{"id":21651,"date":"2022-11-14T15:36:01","date_gmt":"2022-11-14T10:06:01","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/"},"modified":"2022-11-14T15:36:01","modified_gmt":"2022-11-14T10:06:01","slug":"solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/","title":{"rendered":"[Solved] Generate random number of 6 digit length with at-least n unique digits in php"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-47581305\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"47581305\" data-parentid=\"47580890\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<blockquote>\n<p>Asker Comment: 4 unique and 2 repetitive digits. But they should also have 3 unique<br \/>\n  and 3 repetitive digits, 2 unique and 4 repetitive digits in random<br \/>\n  number.<\/p>\n<\/blockquote>\n<p>Ok, I think I <em>finally<\/em> understand, so I have tweaked the algorithm to match <strong>every possible rule<\/strong> you&#8217;re expecting from 0 to 6 unique, <em>though as you will see ive not changed much to my original answers code<\/em>. Ive even added a sort flag so as to sort the int&#8217;s incase that is also a requirement. Im determined!! <\/p>\n<pre><code>&lt;?php\n\/\/\nfunction random_number_with_dupe($len = 6, $dup = 1, $sort = false) {\n    if ($dup &lt; 1) {\n        throw new InvalidArgumentException('Second argument is &lt; 1');\n    }        \n\n    $num = range(0,9);\n    shuffle($num);\n\n    $num = array_slice($num, 0, ($len-$dup)+1);\n\n    if ($dup &gt; 0) {\n        $k = array_rand($num, 1);\n        for ($i=0; $i&lt;($dup-1); $i++) {\n            $num[] = $num[$k];\n        }\n    }\n\n    if ($sort) {\n        sort($num);\n    }\n\n    return implode('', $num);\n}\n<\/code><\/pre>\n<p><strong>All unique.<\/strong><\/p>\n<pre><code>for ($i=0; $i&lt;5; $i++) {\n    echo random_number_with_dupe(6, 1, true).PHP_EOL;\n}\n\/*\n124579\n123679\n013568\n015789\n013578\n*\/\n<\/code><\/pre>\n<p><strong>4 unique and 2 repetitive digits.<\/strong><\/p>\n<pre><code>for ($i=0; $i&lt;5; $i++) {\n    echo random_number_with_dupe(6, 2, true).PHP_EOL;\n}\n\/*\n235699\n037789\n034677\n012249\n033569\n*\/\n<\/code><\/pre>\n<p><strong>3 unique and 3 repetitive digits.<\/strong><\/p>\n<pre><code>for ($i=0; $i&lt;5; $i++) {\n    echo random_number_with_dupe(6, 3, true).PHP_EOL;\n}\n\/*\n015559\n011147\n111239\n677789\n456777\n*\/\n<\/code><\/pre>\n<p><strong>2 unique and 4 repetitive digits.<\/strong><\/p>\n<pre><code>for ($i=0; $i&lt;5; $i++) {\n    echo random_number_with_dupe(6, 4, true).PHP_EOL;\n}\n\/*\n068888\n022229\n018888\n333378\n000058\n*\/\n<\/code><\/pre>\n<p><strong>1 unique and 5 repetitive digits.<\/strong><\/p>\n<pre><code>for ($i=0; $i&lt;5; $i++) {\n    echo random_number_with_dupe(6, 5, true).PHP_EOL;\n}\n\/*\n788888\n066666\n799999\n355555\n244444\n*\/\n<\/code><\/pre>\n<p><strong>0 unique and 6 repetitive digits.<\/strong><\/p>\n<pre><code>for ($i=0; $i&lt;5; $i++) {\n    echo random_number_with_dupe(6, 6, true).PHP_EOL;\n}\n\/*\n888888\n333333\n777777\n888888\n777777\n*\/\n<\/code><\/pre>\n<p><strong>All rules.<\/strong><\/p>\n<pre><code>for ($i=0; $i&lt;6; $i++) {\n    echo random_number_with_dupe(6, $i+1, true).PHP_EOL;\n}\n\/*\n345789\n003569\n245666\n000089\n777778\n555555\n*\/\n<\/code><\/pre>\n<p><strong>All rules (random).<\/strong><\/p>\n<pre><code>for ($i=0; $i&lt;6; $i++) {\n    echo random_number_with_dupe(6, mt_rand(1, 6), true).PHP_EOL;\n}\n\/*\n225678\n222222\n111359\n444444\n777778\n233349\n*\/\n<\/code><\/pre>\n<p><strong>Working example:<\/strong><\/p>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/3v4l.org\/3OCkc\">https:\/\/3v4l.org\/3OCkc<\/a><\/p>\n<h2>Original<\/h2>\n<p>How about this, every number is unique. Use 0 to 9 for the entropy and then shuffle.<\/p>\n<pre><code>&lt;?php\nfunction random_number($len = 6) {\n    $num = range(0,9);\n    shuffle($num);\n    return implode('', array_slice($num, 0, $len));\n}\n\nfor ($i=0; $i&lt;10; $i++) {\n    echo random_number(6).PHP_EOL;\n}\n<\/code><\/pre>\n<p>Result:<\/p>\n<pre><code>357964\n365870\n392576\n285196\n278915\n712960\n751032\n517420\n943257\n380162\n<\/code><\/pre>\n<hr>\n<p><strong>Edit<\/strong> <\/p>\n<blockquote>\n<p>Asker Comment: This I can do but the requirement is different. They need<br \/>\n  at-least 2 unique numbers. with 1 <strong>duplicate<\/strong>.<\/p>\n<\/blockquote>\n<pre><code>&lt;?php\nfunction random_number_with_1_dupe($len = 6) {\n    $num = range(0,9);\n    shuffle($num);\n\n    $num = array_slice($num, 0, $len-1);\n\n    $dup = array_rand($num, 1);\n    $num[] = $num[$dup];\n\n    return implode('', $num);\n}\n\nfor ($i=0; $i&lt;10; $i++) {\n    echo random_number_with_1_dupe(6).PHP_EOL;\n}\n\n564795\n698756\n937414\n760897\n706383\n784626\n520166\n614055\n798057\n295809\n<\/code><\/pre>\n<p><strong>Edit v4.0<\/strong><\/p>\n<blockquote>\n<p>I need a random number without these two rules : 1 unique and 5<br \/>\n  repetitive digits. 0 unique and 6 repetitive digits. Rules applicable<br \/>\n  : All unique. 4 unique and 2 repetitive digits. 3 unique and 3<br \/>\n  repetitive digits. 2 unique and 4 repetitive digits.<\/p>\n<\/blockquote>\n<p>The second argument is the <em>rule<\/em>, so you would to do:<\/p>\n<p><code>echo random_number_with_dupe(6, mt_rand(1, 4), true).PHP_EOL;<\/code><\/p>\n<p>If you want something inbetween you would define a rules array and pick out a random one.<\/p>\n<pre><code>$rules = [\n    1,3,5\n];\n\necho random_number_with_dupe(6, $rules[array_rand($rules)], true);\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">12<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Generate random number of 6 digit length with at-least n unique digits in php <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] 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&#8217;re expecting from 0 to 6 unique, though as &#8230; <a title=\"[Solved] Generate random number of 6 digit length with at-least n unique digits in php\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/\" aria-label=\"More on [Solved] Generate random number of 6 digit length with at-least n unique digits in php\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[339],"class_list":["post-21651","post","type-post","status-publish","format-standard","hentry","category-solved","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Generate random number of 6 digit length with at-least n unique digits in php - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Generate random number of 6 digit length with at-least n unique digits in php - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] 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&#8217;re expecting from 0 to 6 unique, though as ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-14T10:06:01+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Generate random number of 6 digit length with at-least n unique digits in php\",\"datePublished\":\"2022-11-14T10:06:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/\"},\"wordCount\":243,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"php\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/\",\"name\":\"[Solved] Generate random number of 6 digit length with at-least n unique digits in php - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-14T10:06:01+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Generate random number of 6 digit length with at-least n unique digits in php\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Generate random number of 6 digit length with at-least n unique digits in php - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Generate random number of 6 digit length with at-least n unique digits in php - JassWeb","og_description":"[ad_1] 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&#8217;re expecting from 0 to 6 unique, though as ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/","og_site_name":"JassWeb","article_published_time":"2022-11-14T10:06:01+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Generate random number of 6 digit length with at-least n unique digits in php","datePublished":"2022-11-14T10:06:01+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/"},"wordCount":243,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["php"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/","url":"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/","name":"[Solved] Generate random number of 6 digit length with at-least n unique digits in php - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-14T10:06:01+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-generate-random-number-of-6-digit-length-with-at-least-n-unique-digits-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Generate random number of 6 digit length with at-least n unique digits in php"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/21651","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=21651"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/21651\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=21651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=21651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=21651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}