{"id":41,"date":"2009-08-28T16:44:00","date_gmt":"2009-08-28T16:44:00","guid":{"rendered":"http:\/\/graham-kendall.com\/blog\/?p=41"},"modified":"2020-09-22T02:02:01","modified_gmt":"2020-09-22T02:02:01","slug":"random-number-generation","status":"publish","type":"post","link":"https:\/\/graham-kendall.com\/blog\/random-number-generation\/","title":{"rendered":"Random Number Generation"},"content":{"rendered":"<p>We all know that computer generated random numbers are not really random at all, but just pseudo-random. And I know that is a lot of discussion about how best to generate random numbers.<\/p>\n<p>To be honest, I have not taken much notice of this in all the programming that I have done but today I found out that it is important.<\/p>\n<p>Without going into the (boring) details, I have been generating a random number that is either 1 or 2 &#8211; but I generate quite a few very quickly.<\/p>\n<p>For anybody that is interested the code is something like<\/p>\n<p>r = rand() % n +1 [where <span style=\"font-style: italic;\">n<\/span> is the range you are interested in]<\/p>\n<p>The problem is, I was returning the same number <span style=\"font-style: italic;\">every<\/span> time. Only <span style=\"font-style: italic;\">very occasionally<\/span> did I get the odd difference.<\/p>\n<p>If I increased the range (say generating numbers between 1 and 20), this made no difference (I always got the same number).<\/p>\n<p>However, if I put in a delay between each call to rand(), this &#8220;solved&#8221; the problem, but this is not a good solution.<\/p>\n<p>Doing some reading around the subject, I think it is to do with the low\/high order bits when generating the random numbers.<\/p>\n<p>But that does not help me generate a decent distribution when making very frequent calls to rand().<\/p>\n<p>I seem to recall that there is a C++ class called RNG (Random Number Generator) available and I am sure that Numerical Recipes in C will have something to say on the issue, but I need to look into these.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We all know that computer generated random numbers are not really random at all, but just pseudo-random. And I know that is a lot of discussion about how best to generate random numbers. To be honest, I have not taken much notice of this in all the programming that I have done but today I [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[177,47,1],"tags":[111],"class_list":["post-41","post","type-post","status-publish","format-standard","hentry","category-archive","category-random-numbers","category-uncategorized","tag-generate-random-numbers"],"_links":{"self":[{"href":"https:\/\/graham-kendall.com\/blog\/wp-json\/wp\/v2\/posts\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/graham-kendall.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/graham-kendall.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/graham-kendall.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/graham-kendall.com\/blog\/wp-json\/wp\/v2\/comments?post=41"}],"version-history":[{"count":1,"href":"https:\/\/graham-kendall.com\/blog\/wp-json\/wp\/v2\/posts\/41\/revisions"}],"predecessor-version":[{"id":1694,"href":"https:\/\/graham-kendall.com\/blog\/wp-json\/wp\/v2\/posts\/41\/revisions\/1694"}],"wp:attachment":[{"href":"https:\/\/graham-kendall.com\/blog\/wp-json\/wp\/v2\/media?parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/graham-kendall.com\/blog\/wp-json\/wp\/v2\/categories?post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/graham-kendall.com\/blog\/wp-json\/wp\/v2\/tags?post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}