{"id":1809,"date":"2014-09-22T17:29:40","date_gmt":"2014-09-22T16:29:40","guid":{"rendered":"http:\/\/blog.davidkeen.com\/?p=1809"},"modified":"2025-10-23T10:24:32","modified_gmt":"2025-10-22T23:54:32","slug":"generating-random-mobile-numbers-with-scala","status":"publish","type":"post","link":"https:\/\/davidkeen.com\/blog\/2014\/09\/generating-random-mobile-numbers-with-scala\/","title":{"rendered":"Generating random mobile numbers with Scala"},"content":{"rendered":"\n<p>Sometimes in testing we need to generate random data. When it comes to generating mobile numbers it would be helpful if we could be sure they aren&#8217;t allocated to a real person, just in case we accidentally send 1000 text messages to random phone numbers. Luckily <a href=\"http:\/\/stakeholders.ofcom.org.uk\/telecoms\/numbering\/guidance-tele-no\/numbers-for-drama\">Ofcom has reserved ranges<\/a> of numbers for dramatic use that are guaranteed not to be allocated to providers.<\/p>\n\n\n\n<p>Here&#8217;s a little random UK mobile number generator wrapped in an infinite-length iterator:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>val random = new util.Random\nval randomMobile = Iterator.continually(\"447700900\" + f\"${random.nextInt(999)}%03d\")<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">val<\/span><span style=\"color: #D8DEE9FF\"> random <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> util.<\/span><span style=\"color: #8FBCBB\">Random<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">val<\/span><span style=\"color: #D8DEE9FF\"> randomMobile <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">Iterator<\/span><span style=\"color: #D8DEE9FF\">.continually(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">447700900<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">+<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">f<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">${<\/span><span style=\"color: #D8DEE9FF\">random.nextInt(<\/span><span style=\"color: #B48EAD\">999<\/span><span style=\"color: #D8DEE9FF\">)<\/span><span style=\"color: #ECEFF4\">}<\/span><span style=\"color: #A3BE8C\">%03d<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Bonus random MAC address generator<\/h2>\n\n\n\n<p>Sometimes we need MAC addresses too. This will generate strings that look like MAC addresses (6 groups of hex digits) but aren&#8217;t really. Good enough for my purposes:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#2e3440ff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#d8dee9ff;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>val random = new util.Random\nval randomMac = Iterator.continually(Array.fill&#91;String&#93;(6)(f\"${random.nextInt(255)}%02x\").mkString(\":\"))<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki nord\" style=\"background-color: #2e3440ff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #81A1C1\">val<\/span><span style=\"color: #D8DEE9FF\"> random <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #81A1C1\">new<\/span><span style=\"color: #D8DEE9FF\"> util.<\/span><span style=\"color: #8FBCBB\">Random<\/span><\/span>\n<span class=\"line\"><span style=\"color: #81A1C1\">val<\/span><span style=\"color: #D8DEE9FF\"> randomMac <\/span><span style=\"color: #81A1C1\">=<\/span><span style=\"color: #D8DEE9FF\"> <\/span><span style=\"color: #8FBCBB\">Iterator<\/span><span style=\"color: #D8DEE9FF\">.continually(<\/span><span style=\"color: #8FBCBB\">Array<\/span><span style=\"color: #D8DEE9FF\">.fill&#91;<\/span><span style=\"color: #8FBCBB\">String<\/span><span style=\"color: #D8DEE9FF\">&#93;(<\/span><span style=\"color: #B48EAD\">6<\/span><span style=\"color: #D8DEE9FF\">)(<\/span><span style=\"color: #81A1C1\">f<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #ECEFF4\">${<\/span><span style=\"color: #D8DEE9FF\">random.nextInt(<\/span><span style=\"color: #B48EAD\">255<\/span><span style=\"color: #D8DEE9FF\">)<\/span><span style=\"color: #ECEFF4\">}<\/span><span style=\"color: #A3BE8C\">%02x<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\">).mkString(<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #A3BE8C\">:<\/span><span style=\"color: #ECEFF4\">&quot;<\/span><span style=\"color: #D8DEE9FF\">))<\/span><\/span><\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes in testing we need to generate random data. When it comes to generating mobile numbers it would be helpful if we could be sure they aren&#8217;t allocated to a real person, just in case we accidentally send 1000 text messages to random phone numbers. Luckily Ofcom has reserved ranges of numbers for dramatic use [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2643,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[25],"tags":[],"class_list":["post-1809","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/davidkeen.com\/blog\/wp-content\/uploads\/2014\/09\/cube-game-cube-instantaneous-speed-pay-40870.jpeg?fit=1280%2C853&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/posts\/1809","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/comments?post=1809"}],"version-history":[{"count":7,"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/posts\/1809\/revisions"}],"predecessor-version":[{"id":2854,"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/posts\/1809\/revisions\/2854"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/media\/2643"}],"wp:attachment":[{"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/media?parent=1809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/categories?post=1809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davidkeen.com\/blog\/wp-json\/wp\/v2\/tags?post=1809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}