{"id":15154,"date":"2023-01-08T13:12:45","date_gmt":"2023-01-08T18:12:45","guid":{"rendered":"https:\/\/panamahitek.com\/?p=15154"},"modified":"2023-01-08T13:22:05","modified_gmt":"2023-01-08T18:22:05","slug":"generating-random-numbers-with-weighted-probabilities-in-java","status":"publish","type":"post","link":"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/","title":{"rendered":"Generating Random Numbers with Weighted Probability in Java"},"content":{"rendered":"<p style=\"text-align: justify;\">It&#8217;s been a while since my last Java programming post. I&#8217;ve been focusing on <a href=\"https:\/\/panamahitek.com\/category\/python\/\">Python<\/a> and <a href=\"https:\/\/panamahitek.com\/tutoriales-machine-learning\/\">Machine Learning tutorials<\/a> recently, but Java remains my favorite programming language.<\/p>\n<p style=\"text-align: justify;\">Random number selection with weighted probabilities is a common task in programming, especially when working with data that has a specific probability distribution. For example, you may have a list of letters with different probabilities of being selected, or a list of items with different probabilities of being chosen from a menu. In this post, we will explore how to implement a solution for this problem in Java using a simple and intuitive approach.<\/p>\n<h5><strong>Random number selection<\/strong><\/h5>\n<p style=\"text-align: justify;\">A random number is a number that is generated by a computer program or a hardware random number generator in a way that is unpredictable and does not follow a deterministic pattern. The purpose of generating random numbers is to provide a means of introducing an element of chance into a computer program or system.<\/p>\n<p style=\"text-align: justify;\">There are many different ways to generate random numbers, and the quality and characteristics of the random numbers generated can vary significantly depending on the method used. In general, however, a good random number generator should produce numbers that are uniformly distributed over a wide range, are statistically independent of one another, and are not easily predictable.<\/p>\n<p>To generate a random number in Java, you can use the <strong><code>java.util.Random<\/code><\/strong> class. For example, the following code generates a random number between two numbers:<\/p>\n<pre class=\"lang:java decode:true\">  Random random = new Random();\r\n  int min = 0;\r\n  int max = 10;\r\n  int randomNumber = random.nextInt(max - min + 1) + min;<\/pre>\n<p>This code generates a random integer between 0 and 10, inclusive. Each number has an equal probability of being chosen. To test this algorithm, we are going to create a simple proof-of-concept test.<\/p>\n<h5><strong>Generating Random Numbers with Equal Probability<\/strong><\/h5>\n<p style=\"text-align: justify;\">To test the code above, I am going to create a testing script. The code I am going to present here can be found in our <a href=\"https:\/\/github.com\/PanamaHitek\/machine_learning_tutorials\/blob\/main\/java\/MachineLearning_Examples\/src\/main\/java\/com\/panamahitek\/probability\/wighted_probability.java\">Github repository<\/a>.<\/p>\n<p>The first thing I am going to do is to declare a class with the following attributes:<\/p>\n<pre class=\"lang:default decode:true\">private static class Letters {\r\n\r\n        private String letter;\r\n        private int count;\r\n        private double probability;\r\n\r\n        public Letters(String letter, int count, double probability) {\r\n            this.letter = letter;\r\n            this.count = count;\r\n            this.probability = probability;\r\n        }\r\n}<\/pre>\n<p style=\"text-align: justify;\">This class makes possible to create objects with letters, a counter and an specific probability. Now I am going to create a list of 10 letters:<\/p>\n<pre class=\"lang:java decode:true\">        \/\/ Create a list of letters\r\n        List&lt;Letters&gt; letters = new ArrayList&lt;&gt;();\r\n\r\n        \/\/ Add 10 letters to the list with their corresponding count and probability\r\n        letters.add(new Letters(\"A\", 0, 0.2));\r\n        letters.add(new Letters(\"B\", 0, 0.2));\r\n        letters.add(new Letters(\"C\", 0, 0.1));\r\n        letters.add(new Letters(\"D\", 0, 0.1));\r\n        letters.add(new Letters(\"E\", 0, 0.1));\r\n        letters.add(new Letters(\"F\", 0, 0.1));\r\n        letters.add(new Letters(\"G\", 0, 0.05));\r\n        letters.add(new Letters(\"H\", 0, 0.05));\r\n        letters.add(new Letters(\"I\", 0, 0.05));\r\n        letters.add(new Letters(\"J\", 0, 0.05));<\/pre>\n<p>To randomly select a letter, I created this method:<\/p>\n<pre class=\"lang:java decode:true\">    \/**\r\n     * Method for performing selection tests with non-weighted probability.\r\n     *\r\n     * @param letters list of letters to perform tests on\r\n     * @param testCount number of tests to perform\r\n     *\/\r\n    private static void nonWeightedProbability(List&lt;Letters&gt; letters, int testCount) {\r\n        for (int i = 0; i &lt; testCount; i++) {\r\n            \/\/ Generate random number between 0 and size of letters list minus 1\r\n            Random random = new Random();\r\n            int min = 0;\r\n            int max = letters.size() - 1;\r\n            int randomNumber = random.nextInt(max - min + 1) + min;\r\n\r\n            \/\/ Select letter at index of random number and increment its count by 1\r\n            String selectedLetter = letters.get(randomNumber).getLetter();\r\n            letters.stream()\r\n                    .filter(a -&gt; a.getLetter().equals(selectedLetter))\r\n                    .findFirst()\r\n                    .ifPresent(l -&gt; l.setCount(l.getCount() + 1));\r\n        }\r\n\r\n        \/\/ Print count and percentage of each letter\r\n        for (Letters letter : letters) {\r\n            System.out.println(letter.getLetter()\r\n                    + \" -&gt; \" + letter.getCount()\r\n                    + \"(\" + String.format(\"%.2f\", (letter.getCount() * Math.pow(testCount, -1)) * 100) + \"%)\");\r\n        }\r\n\r\n        \/\/ Reset count of each letter to 0\r\n        letters.forEach(a -&gt; a.setCount(0));\r\n    }<\/pre>\n<p style=\"text-align: justify;\">The method performs a loop &#8220;testCount&#8221; number of times. In each iteration of the loop, it generates a random integer between 0 and the size of the &#8220;letters&#8221; list minus 1. Then, it selects the &#8220;Letter&#8221; object at the index of the random number and increments its count by 1.<\/p>\n<p style=\"text-align: justify;\">After the loop finishes executing, the method prints out the count and percentage of each &#8220;Letter&#8221; object. Finally, it resets the count of each &#8220;Letter&#8221; object to 0.<\/p>\n<p>To test this code, you need to make something like this:<\/p>\n<pre class=\"lang:default decode:true\">int testCount = 100000;\r\nnonWeightedProbability(letters, testCount);<\/pre>\n<p>The following output is produced after running the random letter selection 100,000 times:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-15155 aligncenter\" src=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-27.png\" width=\"265\" height=\"220\" srcset=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-27.png 340w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-27-300x249.png 300w\" sizes=\"auto, (max-width: 265px) 100vw, 265px\" \/><\/p>\n<p style=\"text-align: justify;\">As we can see, each letter has been selected approximately 10% of the time, with the results fluctuating slightly above or below this value. This demonstrates that the random letter selection process is generating output with equal probability for each letter.<\/p>\n<h5><strong>Generating Random Numbers with Weighted Probability in Java<\/strong><\/h5>\n<p style=\"text-align: justify;\">Sometimes we may want to generate random numbers with probabilities that are not all equal. For example, we may want one letter to be selected more frequently than the others. In these cases, we can use weighted probabilities. This allows us to specify the probability of selecting a particular element, rather than all elements having an equal probability of being selected.<\/p>\n<p>In the letter list that I created before I assigned a probability for each letter:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-15157 aligncenter\" src=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-28.png\" width=\"577\" height=\"189\" srcset=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-28.png 778w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-28-300x98.png 300w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-28-768x252.png 768w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-28-696x228.png 696w\" sizes=\"auto, (max-width: 577px) 100vw, 577px\" \/><\/p>\n<p style=\"text-align: justify;\">Now, we will create a method similar to the previous one, but the resulting percentages for each letter should be close to the probabilities that were set. Here is the method:<\/p>\n<pre class=\"lang:java decode:true \"> \/**\r\n     *\r\n     * Method for performing selection tests with weighted probability.\r\n     *\r\n     * @param letters list of letters to perform tests on\r\n     * @param testCount number of tests to perform\r\n     *\/\r\n    private static void weightedProbability(List&lt;Letters&gt; letters, int testCount) {\r\n        \/\/Repeat the selection process 'testCount' times\r\n        for (int i = 0; i &lt; testCount; i++) {\r\n            \/\/List to hold the cumulative probabilities of each letter\r\n            List&lt;Double&gt; cumulativeProbabilities = new ArrayList&lt;&gt;();\r\n            double sum = 0;\r\n            \/\/Calculate cumulative probabilities for each letter\r\n            for (Letters l : letters) {\r\n                sum += l.getProbability();\r\n                cumulativeProbabilities.add(sum);\r\n            }\r\n\r\n            \/\/Generate a random number between 0 and 1\r\n            double r = Math.random();\r\n            \/\/Select a letter based on the cumulative probabilities\r\n            String selectedLetter = letters.stream()\r\n                    \/\/Find the first letter whose cumulative probability is greater than the random number\r\n                    .filter(l -&gt; r &lt; cumulativeProbabilities.get(letters.indexOf(l)))\r\n                    .findFirst().get().getLetter();\r\n\r\n            \/\/Increment the count for the selected letter\r\n            letters.stream()\r\n                    .filter(a -&gt; a.getLetter().equals(selectedLetter))\r\n                    .findFirst()\r\n                    .ifPresent(l -&gt; l.setCount(l.getCount() + 1));\r\n        }\r\n        \/\/After 'testCount' loops, print out the number of times each letter was selected and the percentage it represents\r\n        for (Letters letter : letters) {\r\n            System.out.println(letter.getLetter()\r\n                    + \" -&gt; \" + letter.getCount()\r\n                    + \"(\" + String.format(\"%.2f\", (letter.getCount() * Math.pow(testCount, -1)) * 100) + \"%)\");\r\n        }\r\n        \/\/Reset the count for each letter to 0\r\n        letters.forEach(a -&gt; a.setCount(0));\r\n    }\r\n<\/pre>\n<p>The result of running this code is:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-15159 aligncenter\" src=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-29.png\" width=\"251\" height=\"210\" srcset=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-29.png 328w, https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/Pasted-29-300x252.png 300w\" sizes=\"auto, (max-width: 251px) 100vw, 251px\" \/><\/p>\n<p style=\"text-align: justify;\">As seen in the output, the selection frequency for each letter aligns with the probability that was previously set. This demonstrates that our algorithm effectively functions as a weighted random number generator.<\/p>\n<h5><strong>Conclusion<\/strong><\/h5>\n<p style=\"text-align: justify;\">The complete code with both examples can be downloaded from our <a href=\"https:\/\/github.com\/PanamaHitek\/machine_learning_tutorials\/blob\/main\/java\/MachineLearning_Examples\/src\/main\/java\/com\/panamahitek\/probability\/wighted_probability.java\">Github Repository<\/a>.<\/p>\n<p style=\"text-align: justify;\">In conclusion, we have learned how to generate random numbers with equal probability and with weighted probability in Java using the <strong>java.util.Random class<\/strong>. We have also seen how to implement a random selection algorithm with weighted probabilities using a list of objects with a letter, a counter and a probability attribute.<\/p>\n<p style=\"text-align: justify;\">We hope that this post has been helpful to you and if you have any questions or comments, please don&#8217;t hesitate to leave them below. Thank you for reading!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s been a while since my last Java programming post. I&#8217;ve been focusing on Python and Machine Learning tutorials recently, but Java remains my favorite programming language. Random number selection with weighted probabilities is a common task in programming, especially when working with data that has a specific probability distribution. For example, you may have [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":15162,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1972],"tags":[2560,1976,2561,2555,2562,2559,2554,2558,2556,2557],"class_list":{"0":"post-15154","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-java-en","8":"tag-computer-science","9":"tag-java-en","10":"tag-mathematics","11":"tag-probability","12":"tag-probability-distribution","13":"tag-programming","14":"tag-random","15":"tag-random-number-generation","16":"tag-random-selection","17":"tag-weighted-probabilities"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Generating Random Numbers with Weighted Probability in Java<\/title>\n<meta name=\"description\" content=\"In this post, we will explore how to implement a solution for random selection with weighted probability in Java using a simple approach\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Generating Random Numbers with Weighted Probability in Java\" \/>\n<meta property=\"og:description\" content=\"In this post, we will explore how to implement a solution for random selection with weighted probability in Java using a simple approach\" \/>\n<meta property=\"og:url\" content=\"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/\" \/>\n<meta property=\"og:site_name\" content=\"Panama Hitek\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/antony.garcia.gonzalez\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-08T18:12:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-08T18:22:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/weighted_probability.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Antony Garc\u00eda Gonz\u00e1lez\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/antony_garcia_g\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Antony Garc\u00eda Gonz\u00e1lez\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/\"},\"author\":{\"name\":\"Antony Garc\u00eda Gonz\u00e1lez\",\"@id\":\"https:\/\/panamahitek.com\/en\/#\/schema\/person\/a22e06c60a9a3fad2dcddfb25a6fca6e\"},\"headline\":\"Generating Random Numbers with Weighted Probability in Java\",\"datePublished\":\"2023-01-08T18:12:45+00:00\",\"dateModified\":\"2023-01-08T18:22:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/\"},\"wordCount\":754,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/weighted_probability.jpg\",\"keywords\":[\"computer science\",\"Java\",\"mathematics\",\"probability\",\"probability distribution\",\"programming\",\"random\",\"random number generation\",\"random selection\",\"weighted probabilities\"],\"articleSection\":[\"Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/\",\"url\":\"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/\",\"name\":\"Generating Random Numbers with Weighted Probability in Java\",\"isPartOf\":{\"@id\":\"https:\/\/panamahitek.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/weighted_probability.jpg\",\"datePublished\":\"2023-01-08T18:12:45+00:00\",\"dateModified\":\"2023-01-08T18:22:05+00:00\",\"author\":{\"@id\":\"https:\/\/panamahitek.com\/en\/#\/schema\/person\/a22e06c60a9a3fad2dcddfb25a6fca6e\"},\"description\":\"In this post, we will explore how to implement a solution for random selection with weighted probability in Java using a simple approach\",\"breadcrumb\":{\"@id\":\"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/#primaryimage\",\"url\":\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/weighted_probability.jpg\",\"contentUrl\":\"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/weighted_probability.jpg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Portada\",\"item\":\"https:\/\/panamahitek.com\/en\/home\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Generating Random Numbers with Weighted Probability in Java\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/panamahitek.com\/en\/#website\",\"url\":\"https:\/\/panamahitek.com\/en\/\",\"name\":\"Panama Hitek\",\"description\":\"Conocimiento libre, de Panam\u00e1 para el mundo\",\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/panamahitek.com\/en\/#\/schema\/person\/a22e06c60a9a3fad2dcddfb25a6fca6e\",\"name\":\"Antony Garc\u00eda Gonz\u00e1lez\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/panamahitek.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d7a10397454e66de561db7fc2ed37dd2bd115478c378b22eaafd410de973dfd1?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d7a10397454e66de561db7fc2ed37dd2bd115478c378b22eaafd410de973dfd1?s=96&d=mm&r=g\",\"caption\":\"Antony Garc\u00eda Gonz\u00e1lez\"},\"description\":\"Ingeniero Electromec\u00e1nico, egresado de la Universidad Tecnol\u00f3gica de Panam\u00e1. Miembro fundador de Panama Hitek. Entusiasta de la electr\u00f3nica y la programaci\u00f3n.\",\"sameAs\":[\"https:\/\/www.facebook.com\/antony.garcia.gonzalez\",\"https:\/\/www.instagram.com\/antony.garcia.gonzalez\",\"https:\/\/www.linkedin.com\/in\/antony-garcia-gonzalez-96539461\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/antony_garcia_g\"],\"url\":\"https:\/\/panamahitek.com\/en\/author\/agarciag-2\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Generating Random Numbers with Weighted Probability in Java","description":"In this post, we will explore how to implement a solution for random selection with weighted probability in Java using a simple approach","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:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/","og_locale":"en_US","og_type":"article","og_title":"Generating Random Numbers with Weighted Probability in Java","og_description":"In this post, we will explore how to implement a solution for random selection with weighted probability in Java using a simple approach","og_url":"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/","og_site_name":"Panama Hitek","article_author":"https:\/\/www.facebook.com\/antony.garcia.gonzalez","article_published_time":"2023-01-08T18:12:45+00:00","article_modified_time":"2023-01-08T18:22:05+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/weighted_probability.jpg","type":"image\/jpeg"}],"author":"Antony Garc\u00eda Gonz\u00e1lez","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/antony_garcia_g","twitter_misc":{"Written by":"Antony Garc\u00eda Gonz\u00e1lez","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/#article","isPartOf":{"@id":"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/"},"author":{"name":"Antony Garc\u00eda Gonz\u00e1lez","@id":"https:\/\/panamahitek.com\/en\/#\/schema\/person\/a22e06c60a9a3fad2dcddfb25a6fca6e"},"headline":"Generating Random Numbers with Weighted Probability in Java","datePublished":"2023-01-08T18:12:45+00:00","dateModified":"2023-01-08T18:22:05+00:00","mainEntityOfPage":{"@id":"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/"},"wordCount":754,"commentCount":0,"image":{"@id":"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/weighted_probability.jpg","keywords":["computer science","Java","mathematics","probability","probability distribution","programming","random","random number generation","random selection","weighted probabilities"],"articleSection":["Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/","url":"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/","name":"Generating Random Numbers with Weighted Probability in Java","isPartOf":{"@id":"https:\/\/panamahitek.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/#primaryimage"},"image":{"@id":"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/#primaryimage"},"thumbnailUrl":"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/weighted_probability.jpg","datePublished":"2023-01-08T18:12:45+00:00","dateModified":"2023-01-08T18:22:05+00:00","author":{"@id":"https:\/\/panamahitek.com\/en\/#\/schema\/person\/a22e06c60a9a3fad2dcddfb25a6fca6e"},"description":"In this post, we will explore how to implement a solution for random selection with weighted probability in Java using a simple approach","breadcrumb":{"@id":"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/#primaryimage","url":"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/weighted_probability.jpg","contentUrl":"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/weighted_probability.jpg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/panamahitek.com\/en\/generating-random-numbers-with-weighted-probabilities-in-java\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Portada","item":"https:\/\/panamahitek.com\/en\/home\/"},{"@type":"ListItem","position":2,"name":"Generating Random Numbers with Weighted Probability in Java"}]},{"@type":"WebSite","@id":"https:\/\/panamahitek.com\/en\/#website","url":"https:\/\/panamahitek.com\/en\/","name":"Panama Hitek","description":"Conocimiento libre, de Panam\u00e1 para el mundo","inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/panamahitek.com\/en\/#\/schema\/person\/a22e06c60a9a3fad2dcddfb25a6fca6e","name":"Antony Garc\u00eda Gonz\u00e1lez","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/panamahitek.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d7a10397454e66de561db7fc2ed37dd2bd115478c378b22eaafd410de973dfd1?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d7a10397454e66de561db7fc2ed37dd2bd115478c378b22eaafd410de973dfd1?s=96&d=mm&r=g","caption":"Antony Garc\u00eda Gonz\u00e1lez"},"description":"Ingeniero Electromec\u00e1nico, egresado de la Universidad Tecnol\u00f3gica de Panam\u00e1. Miembro fundador de Panama Hitek. Entusiasta de la electr\u00f3nica y la programaci\u00f3n.","sameAs":["https:\/\/www.facebook.com\/antony.garcia.gonzalez","https:\/\/www.instagram.com\/antony.garcia.gonzalez","https:\/\/www.linkedin.com\/in\/antony-garcia-gonzalez-96539461\/","https:\/\/x.com\/https:\/\/twitter.com\/antony_garcia_g"],"url":"https:\/\/panamahitek.com\/en\/author\/agarciag-2\/"}]}},"jetpack_featured_media_url":"https:\/\/panamahitek.com\/wp-content\/uploads\/2023\/01\/weighted_probability.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/panamahitek.com\/en\/wp-json\/wp\/v2\/posts\/15154","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/panamahitek.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/panamahitek.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/panamahitek.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/panamahitek.com\/en\/wp-json\/wp\/v2\/comments?post=15154"}],"version-history":[{"count":0,"href":"https:\/\/panamahitek.com\/en\/wp-json\/wp\/v2\/posts\/15154\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/panamahitek.com\/en\/wp-json\/wp\/v2\/media\/15162"}],"wp:attachment":[{"href":"https:\/\/panamahitek.com\/en\/wp-json\/wp\/v2\/media?parent=15154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/panamahitek.com\/en\/wp-json\/wp\/v2\/categories?post=15154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/panamahitek.com\/en\/wp-json\/wp\/v2\/tags?post=15154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}