{"id":789,"date":"2013-02-01T16:39:29","date_gmt":"2013-02-01T16:39:29","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=789"},"modified":"2020-07-15T09:48:00","modified_gmt":"2020-07-15T09:48:00","slug":"rand","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/random\/rand","title":{"rendered":"rand()"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Geneate Random Value<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/CREATE RANDOM NUMBER\n\t\/\/#include &lt;time.h>\n\tunsigned int iseed = (unsigned int)time(NULL);\t\t\t\/\/Seed srand() using time() otherwise it will start from a default value of 1\n\tsrand (iseed);\n\t#define\tRANDOM_MAX\t\t\t11\t\t\t\t\/\/Random value from 0 - #\n\tint random_value = (int)((1.0 + RANDOM_MAX) * rand() \/ ( RAND_MAX + 1.0 ) );\t\t\/\/Scale rand()'s return value against RAND_MAX using doubles instead of a pure modulus to have a more distributed result.<\/code><\/pre>\n\n\n\n<p><strong>Note: RAND_MAX is not RANDOM_MAX<\/strong><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example functions<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/*********************************************\n\/\/*********************************************\n\/\/********** GENERATE RANDOM INTEGER **********\n\/\/*********************************************\n\/\/*********************************************\n\/\/MaxValue\n\/\/\tRandom value will be from 0 to #\nint GenerateRandomInt (int MaxValue)\n{\n\tunsigned int iseed = (unsigned int)time(NULL);\t\t\t\/\/Seed srand() using time() otherwise it will start from a default value of 1\n\tsrand (iseed);\n\tint random_value = (int)((1.0 + MaxValue) * rand() \/ ( RAND_MAX + 1.0 ) );\t\t\/\/Scale rand()'s return value against RAND_MAX using doubles instead of a pure modulus to have a more distributed result.\n\treturn(random_value);\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Geneate Random Value Note: RAND_MAX is not RANDOM_MAX Example functions<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-789","post","type-post","status-publish","format-standard","hentry","category-random"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/789","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=789"}],"version-history":[{"count":4,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/789\/revisions"}],"predecessor-version":[{"id":3265,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/789\/revisions\/3265"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}