{"id":33709,"date":"2023-02-12T19:16:18","date_gmt":"2023-02-12T13:46:18","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/"},"modified":"2023-02-12T19:16:18","modified_gmt":"2023-02-12T13:46:18","slug":"solved-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/","title":{"rendered":"[Solved] How to improve the uniformly distributed of a given random function to generate uniformly distributed numbers? [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-66368271\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"66368271\" data-parentid=\"66350341\" data-score=\"3\" 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>what can be improved on a given random function to make it more random or for a bigger range or something else?<\/p>\n<p>and therefore no SRANDOM can be used up to now.<\/p>\n<p>How to improve the randomness of the fuction above, if possible ?<\/p>\n<\/blockquote>\n<p>Sooo write your own SRANDOM with your own semantics. Ex:<\/p>\n<pre><code>srandom() {\n   # take random number from \/dev\/urandom\n   # we take only just 4 bytes - one 2^32 number\n   printf \"%d\\n\" \"0x$(\n       dd if=\/dev\/urandom of=\/dev\/stdout bs=4 count=1 status=none |\n       xxd -p)\"\n}\n<\/code><\/pre>\n<p>and then:<\/p>\n<pre><code>normalize_value(){\n   ...\n   rnd=$(srandom)\n   rnd_count=$((rnd \/ ...))\n}\n<\/code><\/pre>\n<blockquote>\n<p>Accepting a wider range of numbers<\/p>\n<\/blockquote>\n<p>If you are not happy with the way shell arithmetic expansion works, then&#8230; use a different tool. <code>bc<\/code> calculator has unlimited range.<\/p>\n<pre><code>rnd_count=$(echo \"\n     # see https:\/\/superuser.com\/questions\/31445\/gnu-bc-modulo-with-scale-other-than-0\n     scale=0; \n     # super big random number from three 2^32 numbers\n     rnd = $(srandom) * 2^(32*2) +  $(srandom) * 2^32 + $(srandom)\n     rnd % ($max - $min + 1) + $min\n     \" | bc)\n<\/code><\/pre>\n<p>You can write your own C program with <code>getrandom()<\/code> and compile it on the fly <code>echo \"int main() { stuff(); }\" | gcc -xc - &amp;&amp; .\/a.out; rm .\/a.out<\/code> basically granting you any semantics you want. There are also other scripting languages, like perl, python, ruby, all most probably with their own big-number libraries and urandom number generation implementations. Sky the limit.<\/p>\n<blockquote>\n<p>Every improvement should use bash only.<\/p>\n<\/blockquote>\n<p>Is from my perspective a pointless limitation &#8211; overall, I am paid for results, not really &#8220;how&#8221; I solve problems. Anyway, you could, giving you a bunch of ideas how to proceed:<\/p>\n<ul>\n<li>First write a function that would read from <code>\/dev\/urandom<\/code> and convert the bytes into a number.\n<ul>\n<li>I have no good idea how to do it in pure bash while keeping the randomness at sane levels. I suspect the input will drain fast.<\/li>\n<li>You could read one byte from urandom. You&#8217;ll have to ignore <code>read<\/code> exit status, cause the byte may be zero byte or newline.<\/li>\n<li>Then check if that byte is a digit. If it&#8217;s not, repeat previous step.<\/li>\n<li>Treat such algorithm treat as a generator of random number within the range of 0-9. Build bigger numbers from those digits.<\/li>\n<\/ul>\n<\/li>\n<li>Then develop your own big-number library using arithmetic expansion as a &#8220;backend&#8221;, written in bash.\n<ul>\n<li>Seems pretty pointless, because <code>bc<\/code> is commonly available.<\/li>\n<li>This would work as usual big number libraries do.<\/li>\n<li>I suggest to store the number as an array of number at max 2^16. For inspiration, research similar libraries written in C and C++ languages, and convert it to bash.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How to improve the uniformly distributed of a given random function to generate uniformly distributed numbers? [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] what can be improved on a given random function to make it more random or for a bigger range or something else? and therefore no SRANDOM can be used up to now. How to improve the randomness of the fuction above, if possible ? Sooo write your own SRANDOM with your own semantics. Ex: &#8230; <a title=\"[Solved] How to improve the uniformly distributed of a given random function to generate uniformly distributed numbers? [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/\" aria-label=\"More on [Solved] How to improve the uniformly distributed of a given random function to generate uniformly distributed numbers? [closed]\">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":[1042,413,424,1014,5999],"class_list":["post-33709","post","type-post","status-publish","format-standard","hentry","category-solved","tag-bash","tag-function","tag-linux","tag-random","tag-random-seed"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] How to improve the uniformly distributed of a given random function to generate uniformly distributed numbers? [closed] - 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-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How to improve the uniformly distributed of a given random function to generate uniformly distributed numbers? [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] what can be improved on a given random function to make it more random or for a bigger range or something else? and therefore no SRANDOM can be used up to now. How to improve the randomness of the fuction above, if possible ? Sooo write your own SRANDOM with your own semantics. Ex: ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-12T13:46:18+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-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How to improve the uniformly distributed of a given random function to generate uniformly distributed numbers? [closed]\",\"datePublished\":\"2023-02-12T13:46:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/\"},\"wordCount\":365,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"bash\",\"function\",\"linux\",\"random\",\"random-seed\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/\",\"name\":\"[Solved] How to improve the uniformly distributed of a given random function to generate uniformly distributed numbers? [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-02-12T13:46:18+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How to improve the uniformly distributed of a given random function to generate uniformly distributed numbers? [closed]\"}]},{\"@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=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How to improve the uniformly distributed of a given random function to generate uniformly distributed numbers? [closed] - 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-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How to improve the uniformly distributed of a given random function to generate uniformly distributed numbers? [closed] - JassWeb","og_description":"[ad_1] what can be improved on a given random function to make it more random or for a bigger range or something else? and therefore no SRANDOM can be used up to now. How to improve the randomness of the fuction above, if possible ? Sooo write your own SRANDOM with your own semantics. Ex: ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/","og_site_name":"JassWeb","article_published_time":"2023-02-12T13:46:18+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-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How to improve the uniformly distributed of a given random function to generate uniformly distributed numbers? [closed]","datePublished":"2023-02-12T13:46:18+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/"},"wordCount":365,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["bash","function","linux","random","random-seed"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/","name":"[Solved] How to improve the uniformly distributed of a given random function to generate uniformly distributed numbers? [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-02-12T13:46:18+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-improve-the-uniformly-distributed-of-a-given-random-function-to-generate-uniformly-distributed-numbers-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How to improve the uniformly distributed of a given random function to generate uniformly distributed numbers? [closed]"}]},{"@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=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","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\/33709","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=33709"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/33709\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=33709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=33709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=33709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}