{"id":19457,"date":"2022-11-06T19:04:48","date_gmt":"2022-11-06T13:34:48","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-does-rand-work-in-c-closed\/"},"modified":"2022-11-06T19:04:48","modified_gmt":"2022-11-06T13:34:48","slug":"solved-how-does-rand-work-in-c-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-does-rand-work-in-c-closed\/","title":{"rendered":"[Solved] How does rand() work in C? [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-33380491\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"33380491\" data-parentid=\"33380282\" data-score=\"12\" 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<p>You should know that the easiest way to get information about the C standard library is by using manual pages on a Linux\/UNIX system.<\/p>\n<p>Manual chapter <code>3<\/code> is where you&#8217;ll find manual pages regarding the standard library. To get documentation for <code>rand<\/code>, type <code>man 3 rand<\/code> at a shell prompt.<\/p>\n<p>In case you don&#8217;t have the manual pages handy, I&#8217;ll just quote from the description here:<\/p>\n<pre><code>DESCRIPTION\n\n   The  rand()  function returns a pseudo-random integer in the range 0 to\n   RAND_MAX inclusive (i.e., the mathematical range [0, RAND_MAX]).\n\n   The srand() function sets its argument as the seed for a  new  sequence\n   of  pseudo-random  integers  to be returned by rand().  These sequences\n   are repeatable by calling srand() with the same seed value.\n\n   If no seed value is provided,  the  rand()  function  is  automatically\n   seeded with a value of 1.\n\n   The function rand() is not reentrant or thread-safe, since it uses hid\u2010\n   den state that is modified on each call.  This might just be  the  seed\n   value to be used by the next call, or it might be something more elabo\u2010\n   rate.  In order to get reproducible behavior in a threaded application,\n   this  state must be made explicit; this can be done using the reentrant\n   function rand_r().\n\n   Like rand(), rand_r() returns a  pseudo-random  integer  in  the  range\n   [0, RAND_MAX].  The seedp argument is a pointer to an unsigned int that\n   is used to store state between calls.  If rand_r() is called  with  the\n   same  initial value for the integer pointed to by seedp, and that value\n   is not modified between calls, then  the  same  pseudo-random  sequence\n   will result.\n\n   The  value pointed to by the seedp argument of rand_r() provides only a\n   very small amount of state, so this function will be a weak pseudo-ran\u2010\n   dom generator.  Try drand48_r(3) instead.\n\nRETURN VALUE\n\n   The rand() and rand_r() functions return a value between 0 and RAND_MAX\n   (inclusive).  The srand() function returns no value.\n<\/code><\/pre>\n<p>If you want to know how the code itself works, I can break it down for you:<\/p>\n<pre><code>#include &lt;stdio.h&gt;\n<\/code><\/pre>\n<p>Includes the standard I\/O libraries. (needed in this case to get the definition of <code>printf()<\/code>.)<\/p>\n<pre><code>#include &lt;stdlib.h&gt;\n<\/code><\/pre>\n<p>Includes the C standard library (for the current platform). This will be needed to get the definition of <code>rand()<\/code>, <code>srand()<\/code>, and <code>time()<\/code>.<\/p>\n<pre><code>int main()\n<\/code><\/pre>\n<p>Defines the function <code>main()<\/code>, which will be sought out as the entry point for your compiled executable file at a later date.<\/p>\n<pre><code>{\n<\/code><\/pre>\n<p>Defines the scope of <code>main()<\/code>.<\/p>\n<pre><code>   int i, n;\n<\/code><\/pre>\n<p>Allocates memory space on the stack for two <code>int<\/code> variables, <code>i<\/code>, and <code>n<\/code>.<\/p>\n<pre><code>   time_t t;\n<\/code><\/pre>\n<p>Allocates memory space on the stack for a <code>time_t<\/code> structure, which will be used later for the current time.<\/p>\n<pre><code>   n = 5;\n<\/code><\/pre>\n<p>Initializes <code>int n<\/code> to the value 5.<\/p>\n<pre><code>   \/* Intializes random number generator *\/\n   srand((unsigned) time(&amp;t));\n<\/code><\/pre>\n<p>First calls <code>time()<\/code> with the argument being the memory address of the <code>time_t<\/code> allocated on the stack earlier. This will populate the <code>time_t<\/code> with the current time. (which is actually just expressed as an integer &#8211; the number of seconds since the &#8220;epoch&#8221; &#8211; see <code>man 2 time<\/code>) <\/p>\n<p>Next, calls <code>srand()<\/code> with that value (after casting it to <code>unsigned int<\/code>, which presumably avoids a compiler warning, but is probably safe if you know that <code>sizeof(time_t) &gt;= sizeof(int)<\/code>.). Calling <code>srand()<\/code> here will seed the PRNG (pseudo-random number generator).<\/p>\n<pre><code>   \/* Print 5 random numbers from 0 to 49 *\/\n   for( i = 0 ; i &lt; n ; i++ ) \n   {\n<\/code><\/pre>\n<p>In the <code>for<\/code> statement (in my own words), you&#8217;ll see <code>&lt;initialization-instruction&gt; ; &lt;condition&gt; ; &lt;loop-instruction&gt;<\/code>. The contents of the loop are executed while <code>&lt;condition&gt;<\/code> is true (which, in C terms, means non-zero, but that&#8217;s a separate discussion).<\/p>\n<p>Set the <code>int i<\/code> defined earlier to zero. While <code>i<\/code> is less than <code>n<\/code>, do the operations within the curly braces (<code>{ ... }<\/code>), then increment <code>i<\/code> (<code>++i<\/code>) and check the condition again. (<code>n<\/code> was defined to be <code>5<\/code> earlier, so loop though the values <code>[0, 1, 2, 3, 4]<\/code>)<\/p>\n<pre><code>      printf(\"%d\\n\", rand() % 50);\n<\/code><\/pre>\n<p>The <code>printf()<\/code> call uses format strings to print its output. The <code>%d<\/code> means you want <code>printf<\/code> to print out the first parameter assuming that it is an unsigned decimal integer. If you include more format strings in the <code>printf<\/code> call, you should include multiple corresponding parameters to the <code>printf()<\/code> function.<\/p>\n<p>The <code>\\n<\/code>, when it appears in a character or string constant, is a linefeed. So after the number is printed, the cursor will move to the beginning of the next line.<\/p>\n<p>Finally, <code>rand() % 50<\/code> means &#8220;call <code>rand()<\/code>, divide the result by <code>50<\/code>, and then take the remainder&#8221;. (The <code>%<\/code> is the <em>modulus<\/em> operator, which means you want the remainder.) For example, if <code>rand()<\/code> returns 1000, you&#8217;ll get <code>0<\/code> printed to the screen, since <code>1000 % 50 == 0<\/code> (that is, 1000 divided by 50 equals 20, remainder 0).<\/p>\n<p>So the end result will be printing a pseudo-random value between 0 and 49.<\/p>\n<pre><code>   }\n\n   return(0);\n<\/code><\/pre>\n<p>This will cause <code>main()<\/code> to finish executing and return to its caller (probably whatever code loads binaries on your operating system). It will also most likely cause the exit status of your program to be <code>0<\/code>.<\/p>\n<pre><code>}\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">0<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How does rand() work in C? [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] You should know that the easiest way to get information about the C standard library is by using manual pages on a Linux\/UNIX system. Manual chapter 3 is where you&#8217;ll find manual pages regarding the standard library. To get documentation for rand, type man 3 rand at a shell prompt. In case you don&#8217;t &#8230; <a title=\"[Solved] How does rand() work in C? [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-does-rand-work-in-c-closed\/\" aria-label=\"More on [Solved] How does rand() work in C? [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":[324],"class_list":["post-19457","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] How does rand() work in C? [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-does-rand-work-in-c-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How does rand() work in C? [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] You should know that the easiest way to get information about the C standard library is by using manual pages on a Linux\/UNIX system. Manual chapter 3 is where you&#8217;ll find manual pages regarding the standard library. To get documentation for rand, type man 3 rand at a shell prompt. In case you don&#8217;t ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-does-rand-work-in-c-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-06T13:34:48+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=\"4 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-does-rand-work-in-c-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-does-rand-work-in-c-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How does rand() work in C? [closed]\",\"datePublished\":\"2022-11-06T13:34:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-does-rand-work-in-c-closed\/\"},\"wordCount\":493,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-does-rand-work-in-c-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-does-rand-work-in-c-closed\/\",\"name\":\"[Solved] How does rand() work in C? [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-06T13:34:48+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-does-rand-work-in-c-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-does-rand-work-in-c-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-does-rand-work-in-c-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How does rand() work in C? [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 does rand() work in C? [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-does-rand-work-in-c-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How does rand() work in C? [closed] - JassWeb","og_description":"[ad_1] You should know that the easiest way to get information about the C standard library is by using manual pages on a Linux\/UNIX system. Manual chapter 3 is where you&#8217;ll find manual pages regarding the standard library. To get documentation for rand, type man 3 rand at a shell prompt. In case you don&#8217;t ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-does-rand-work-in-c-closed\/","og_site_name":"JassWeb","article_published_time":"2022-11-06T13:34:48+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-how-does-rand-work-in-c-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-does-rand-work-in-c-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How does rand() work in C? [closed]","datePublished":"2022-11-06T13:34:48+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-does-rand-work-in-c-closed\/"},"wordCount":493,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-does-rand-work-in-c-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-how-does-rand-work-in-c-closed\/","name":"[Solved] How does rand() work in C? [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-06T13:34:48+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-does-rand-work-in-c-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-does-rand-work-in-c-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-does-rand-work-in-c-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How does rand() work in C? [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\/19457","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=19457"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/19457\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=19457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=19457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=19457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}