{"id":1458,"date":"2015-07-25T00:01:16","date_gmt":"2015-07-25T07:01:16","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1458"},"modified":"2015-07-18T10:33:04","modified_gmt":"2015-07-18T17:33:04","slug":"random-numbers-with-decimals","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1458","title":{"rendered":"Random Numbers with Decimals"},"content":{"rendered":"<p>The <em>rand()<\/em> function returns a pseudo-random number as a <em>long int<\/em> value. That value helps your code generate random numbers in the <em>long int<\/em> range. It can also be manipulated to yield random real number values, but that process involves . . . <em>math<\/em>.<br \/>\n<!--more--><br \/>\nThe <em>rand()<\/em> function is prototyped in the <code>stdlib.h<\/code> header file. It generates values in the range of 0 to <code>RAND_MAX<\/code>, which is also defined in <code>stdlib.h<\/code>.<\/p>\n<p>On my system, I peeked inside <code>stdlib.h<\/code> to see how <code>RAND_MAX<\/code> is defined. Here it is:<\/p>\n<pre><code>#define RAND_MAX        0x7fffffff<\/code><\/pre>\n<p>The definition might be different on your system, but the constant name is identical.<\/p>\n<p>You can use the <code>RAND_MAX<\/code> value to help your code generate floating point random values. It makes for a nice, clean equation, as shown in Figure 1.<\/p>\n<div id=\"attachment_1461\" style=\"width: 170px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1461\" src=\"http:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2015\/07\/0725-figure1.png\" alt=\"Figure 1.\" width=\"160\" height=\"75\" class=\"size-full wp-image-1461\" \/><p id=\"caption-attachment-1461\" class=\"wp-caption-text\">Figure 1.<\/p><\/div>\n<p>The value returned by <em>rand()<\/em> is in the range of 0 to <code>RAND_MAX<\/code>. When its divided by <code>RAND_MAX<\/code>, the result is in the range of 0.0 to 1.0.<\/p>\n<p>If you leave the division as-is, then the result is a <em>long int<\/em> value. That means 99 percent of the time the result is 0. Only when the value returned by <em>rand()<\/em> is close to <code>RAND_MAX<\/code> would it equal 1. So the equation must be typecast to a <em>float<\/em>, as is done in the following code at Line 13:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;time.h&gt;\r\n\r\nint main()\r\n{\r\n    int x;\r\n    float fr;\r\n\r\n    srand((unsigned)time(NULL));\r\n    for(x=0;x&lt;10;x++)\r\n    {\r\n        fr = (float)rand()\/RAND_MAX;\r\n        printf(\"%f\\n\",fr);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>At Line 13, the value in variable <code>fr<\/code> ranges between 0.0 and 1.0. Here&#8217;s sample output:<\/p>\n<pre><code>0.581146\r\n0.320701\r\n0.013958\r\n0.597070\r\n0.954065\r\n0.965519\r\n0.479583\r\n0.355317\r\n0.810573\r\n0.297443<\/code><\/pre>\n<p>While these may not be the floating point values your code needs, they do provide a base. You can manipulate the real random numbers to represent values in any range. For example, multiply the values by 100 to have the code generate floating point numbers between 0.0 and 100.0.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Generate a <em>float<\/em> random number. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1458\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1458","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1458","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1458"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1458\/revisions"}],"predecessor-version":[{"id":1481,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1458\/revisions\/1481"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1458"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1458"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1458"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}