{"id":25119,"date":"2022-12-08T00:37:21","date_gmt":"2022-12-07T19:07:21","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/"},"modified":"2022-12-08T00:37:21","modified_gmt":"2022-12-07T19:07:21","slug":"solved-random-in-java-often-generating-same-value","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/","title":{"rendered":"[Solved] Random in Java often generating same value"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-49515747\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"49515747\" data-parentid=\"49490681\" data-score=\"2\" data-position-on-page=\"2\" data-highest-scored=\"0\" data-question-has-accepted-highest-score=\"0\" itemprop=\"suggestedAnswer\" 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<p>The <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.m.wikipedia.org\/wiki\/Birthday_problem\">Birthday Paradox<\/a> predicts that the probability of duplicates in random numbers is much higher than you&#8217;d think. For example, it predicts that, with a mere 23 random people, there&#8217;s a greater than 50% chance of two of them have the same birthday. By the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Pigeonhole_principle\">Pigeonhole Principle<\/a>, it takes 367 people for there to be a 100% chance of a duplicate, but the probability of a duplicate is extremely high even before that.<\/p>\n<p>Here&#8217;s the probability distribution (from Wikipedia):<br \/>\n<a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/12\/Solved-Random-in-Java-often-generating-same-value.png\"><img decoding=\"async\" src=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/12\/Solved-Random-in-Java-often-generating-same-value.png\" alt=\"enter image description here\"><\/a><\/p>\n<p>The <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.m.wikipedia.org\/wiki\/Birthday_problem#Square_approximation\">rule of thumb<\/a> to approximate the number of numbers you have to generate before the probability of duplicates reaches <code>sqrt(2m * p(n))<\/code>, where <code>m<\/code> is the number of possible random numbers and <code>p(n)<\/code> is the probability that you&#8217;re looking for. So, for example, if you&#8217;re generating random numbers in a 50-number range (e.g. if you pick a random number from 100 &#8211; 150), you&#8217;d only have to generate approximately <code>sqrt((2 * 50) * 0.5) = 7.07<\/code> random numbers before the odds are just as good as not that you have a duplicate. If you generate 8 random numbers within a 50-number range, odds are better than not that&#8217;ll have a duplicate. (Note that this only works for <code>p(n)<\/code> values of up to 1\/2).<\/p>\n<p>In your case, there are 8 possible values for any particular random value (5, 6, 7, 8, 9, 10, 11, 12), so you only need to generate <code>sqrt(8) = 2.83<\/code> numbers before there&#8217;s a probability of 50% that you&#8217;ll have a duplicate. In other words, the Birthday Paradox predicts you only need to generate approximately 3 numbers for the chances to be better than not that you&#8217;ll have a duplicate.<\/p>\n<p>See also <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/softwareengineering.stackexchange.com\/questions\/339125\/acceptable-to-rely-on-random-ints-being-unique\">this Q&amp;A<\/a>.<\/p>\n<p>One more point: beware the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Gambler's_fallacy\">Gambler&#8217;s Fallacy<\/a>, in which people assume that if you randomly generate, for example, a 10, odds are the next one won&#8217;t be a 10. Actually, given that you&#8217;re generating random numbers, the odds of any particular number is 1\/8 regardless of what numbers came before. In other words, if you generate a 12, the probability of the next number being a 10 is 1\/8. If you generate a 7, the odds of the next number being a 10 is 1\/8. If you generate a 10, the probability of the next number being a 10 is still 1\/8. Each number is an <em>independent event<\/em> (i.e. the numbers you&#8217;ve generated so far don&#8217;t influence the probability distribution of future numbers in the least).<\/p>\n<p><strong>TL;DR<\/strong> You need to generate much fewer numbers than you think you do before you&#8217;re likely to start getting duplicates &#8211; if you&#8217;re generating random numbers within a small range of numbers in particular (like you are) the number is particularly low.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Random in Java often generating same value <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] The Birthday Paradox predicts that the probability of duplicates in random numbers is much higher than you&#8217;d think. For example, it predicts that, with a mere 23 random people, there&#8217;s a greater than 50% chance of two of them have the same birthday. By the Pigeonhole Principle, it takes 367 people for there to &#8230; <a title=\"[Solved] Random in Java often generating same value\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/\" aria-label=\"More on [Solved] Random in Java often generating same value\">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":[452,323,1014],"class_list":["post-25119","post","type-post","status-publish","format-standard","hentry","category-solved","tag-android","tag-java","tag-random"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Random in Java often generating same value - 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-random-in-java-often-generating-same-value\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Random in Java often generating same value - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] The Birthday Paradox predicts that the probability of duplicates in random numbers is much higher than you&#8217;d think. For example, it predicts that, with a mere 23 random people, there&#8217;s a greater than 50% chance of two of them have the same birthday. By the Pigeonhole Principle, it takes 367 people for there to ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-07T19:07:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/12\/Solved-Random-in-Java-often-generating-same-value.png\" \/>\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-random-in-java-often-generating-same-value\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Random in Java often generating same value\",\"datePublished\":\"2022-12-07T19:07:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/\"},\"wordCount\":438,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/12\/Solved-Random-in-Java-often-generating-same-value.png\",\"keywords\":[\"android\",\"java\",\"random\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/\",\"name\":\"[Solved] Random in Java often generating same value - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/12\/Solved-Random-in-Java-often-generating-same-value.png\",\"datePublished\":\"2022-12-07T19:07:21+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/#primaryimage\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/12\/Solved-Random-in-Java-often-generating-same-value.png\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/12\/Solved-Random-in-Java-often-generating-same-value.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Random in Java often generating same value\"}]},{\"@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] Random in Java often generating same value - 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-random-in-java-often-generating-same-value\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Random in Java often generating same value - JassWeb","og_description":"[ad_1] The Birthday Paradox predicts that the probability of duplicates in random numbers is much higher than you&#8217;d think. For example, it predicts that, with a mere 23 random people, there&#8217;s a greater than 50% chance of two of them have the same birthday. By the Pigeonhole Principle, it takes 367 people for there to ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/","og_site_name":"JassWeb","article_published_time":"2022-12-07T19:07:21+00:00","og_image":[{"url":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/12\/Solved-Random-in-Java-often-generating-same-value.png","type":"","width":"","height":""}],"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-random-in-java-often-generating-same-value\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Random in Java often generating same value","datePublished":"2022-12-07T19:07:21+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/"},"wordCount":438,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"image":{"@id":"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/#primaryimage"},"thumbnailUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/12\/Solved-Random-in-Java-often-generating-same-value.png","keywords":["android","java","random"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/","url":"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/","name":"[Solved] Random in Java often generating same value - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"primaryImageOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/#primaryimage"},"image":{"@id":"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/#primaryimage"},"thumbnailUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/12\/Solved-Random-in-Java-often-generating-same-value.png","datePublished":"2022-12-07T19:07:21+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/#primaryimage","url":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/12\/Solved-Random-in-Java-often-generating-same-value.png","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/uploads\/2022\/12\/Solved-Random-in-Java-often-generating-same-value.png"},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-random-in-java-often-generating-same-value\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Random in Java often generating same value"}]},{"@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\/25119","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=25119"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/25119\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=25119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=25119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=25119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}