{"id":131,"date":"2013-10-29T00:11:01","date_gmt":"2013-10-29T05:11:01","guid":{"rendered":"http:\/\/eastfist.com\/qt_tutorials\/?p=131"},"modified":"2018-04-12T03:42:31","modified_gmt":"2018-04-12T09:42:31","slug":"how-to-generate-random-number-in-qt","status":"publish","type":"post","link":"https:\/\/eastfist.com\/qt_tutorials\/how-to-generate-random-number-in-qt\/","title":{"rendered":"How to generate random number in Qt"},"content":{"rendered":"<p>If you thought Qt provided you with a robust count of math algorithms already, you&#8217;ll be in for a surprise. There is <code><a href=\"http:\/\/qt-project.org\/doc\/qt-5.0\/qtcore\/qtglobal.html#qrand\">qrand()<\/a><\/code>, but it doesn&#8217;t quite provide you with the user-friendliness of other languages. For example, sometimes you want a random number that is negative. Sure, you can do this all manually, but then you&#8217;d have to write your own function. Unfortunately, that&#8217;s exactly what you&#8217;ll have to do.<\/p>\n<p><a href=\"http:\/\/developer.nokia.com\/Community\/Wiki\/Generating_random-value_integers_in_Qt\">http:\/\/developer.nokia.com\/Community\/Wiki\/Generating_random-value_integers_in_Qt<\/a><\/p>\n<pre>#include &lt;QGlobal.h&gt;\r\n#include &lt;QTime&gt;\r\n\r\nint QMyClass::randInt(int low, int high)\r\n{\r\n\/\/ Random number between low and high\r\nreturn qrand() % ((high + 1) - low) + low;\r\n}\r\n\r\n\/\/ Create seed for the random\r\n\/\/ That is needed only once on application startup\r\nQTime time = QTime::currentTime();\r\nqsrand((uint)time.msec());\r\n\r\n\/\/ Get random value between 0-100\r\nint randomValue = randInt(0,100);<\/pre>\n<p>It is recommended that you don&#8217;t use <code>qrand()<\/code> by default if you want to generate encryption-level random numbers.<\/p>\n<p>So, with that code, now you can set a range of numbers which you can pull random numbers from. Make note that qsrand() only sets the seed and you should only do that once.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you thought Qt provided you with a robust count of math algorithms already, you&#8217;ll be in for a surprise. There is qrand(), but it doesn&#8217;t quite provide you with the user-friendliness of other languages. For example, sometimes you want a random number that is negative. Sure, you can do this all manually, but then &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/eastfist.com\/qt_tutorials\/how-to-generate-random-number-in-qt\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;How to generate random number in Qt&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[22,85,37,84,79,58,86,81,82,83,3,80],"class_list":["post-131","post","type-post","status-publish","format-standard","hentry","category-qt-2","tag-c","tag-code","tag-custom","tag-function","tag-generate","tag-howto","tag-manual","tag-numbers","tag-qrand","tag-qsrand","tag-qt","tag-random"],"_links":{"self":[{"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/posts\/131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/comments?post=131"}],"version-history":[{"count":8,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/posts\/131\/revisions"}],"predecessor-version":[{"id":139,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/posts\/131\/revisions\/139"}],"wp:attachment":[{"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/media?parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/categories?post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eastfist.com\/qt_tutorials\/wp-json\/wp\/v2\/tags?post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}