<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>rand function</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/23054/rand-function</link><description> 
I would like to control the output of the below random function. I
want that it return a number between the min and the max parameters.
but it didn&amp;#39;t. can you tell what should I change to do that. 
in the web, I found that 10000 should be the maximun</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: rand function</title><link>https://community.arm.com/thread/141908?ContentTypeID=1</link><pubDate>Fri, 14 Dec 2007 07:39:38 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:fe45934c-20d8-4198-a6f2-99eff92394c7</guid><dc:creator>erik  malund</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;Of course, the price to pay is additional computational effort
(multiplication and division take a while on a &amp;#39;51, especially with
long integers).&lt;/i&gt;&lt;br /&gt;
he clearly does not care about that, he uses an int for TRUE/FALSE.
He crossposted at the SILabs forum &lt;a href="http://www.cygnal.org/ubb/Forum1/HTML/002446.html"&gt;www.cygnal.org/.../002446.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;
Erik&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: rand function</title><link>https://community.arm.com/thread/138632?ContentTypeID=1</link><pubDate>Fri, 14 Dec 2007 07:02:19 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:eea625c4-7e0f-44ec-8ec4-9b9516996627</guid><dc:creator>Christoph Franck</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;I understand what you said about probability of occuring. can
you tell me the way to make it right?&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Somewhat better in terms of distribution would be:&lt;/p&gt;

&lt;pre&gt;
long tmp;

tmp = my_rand();
tmp = tmp * (max - min);
tmp = (tmp + (MAX_RAND / 2)) / MAX_RAND;
tmp = tmp + min;
&lt;/pre&gt;

&lt;p&gt;
Of course, the price to pay is additional computational effort
(multiplication and division take a while on a &amp;#39;51, especially with
long integers).&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: rand function</title><link>https://community.arm.com/thread/130234?ContentTypeID=1</link><pubDate>Fri, 14 Dec 2007 04:53:26 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:67948b78-baf1-4d2b-902a-c92b14a35cf3</guid><dc:creator>Christ Raoul</dc:creator><description>&lt;p&gt;&lt;p&gt;
I use this function to simulate a graphic chart, I just want that
values change each time. I understand what you said about probability
of occuring. can you tell me the way to make it right?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: rand function</title><link>https://community.arm.com/thread/124320?ContentTypeID=1</link><pubDate>Fri, 14 Dec 2007 03:30:49 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:266181b7-aeea-4496-bed1-f7947722c073</guid><dc:creator>Christoph Franck</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;I took the code on the web.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Don&amp;#39;t blindly take code from the web without understanding what it
does.&lt;/p&gt;

&lt;pre&gt;
res = ( my_rand()%(max-min) ) + min;
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
&lt;i&gt;did exactly what I want.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Are you &lt;b&gt;really sure&lt;/b&gt; it does exactly what you want ? The
above line will &lt;b&gt;not&lt;/b&gt; produce pseudorandom numbers that are
evenly distributed (i.e. each of the numbers has the same probability
of occurring). It&amp;#39;s like rolling loaded dice.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: rand function</title><link>https://community.arm.com/thread/113495?ContentTypeID=1</link><pubDate>Thu, 13 Dec 2007 23:59:24 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:51f741a1-9057-4b1b-a109-4e3e81762945</guid><dc:creator>Christ Raoul</dc:creator><description>&lt;p&gt;&lt;p&gt;
sorry for the wrong code and thanks for your answer. I took the
code on the web.&lt;br /&gt;
res = ( my_rand()%(max-min) ) + min; did exactly what I want.
thanks&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: rand function</title><link>https://community.arm.com/thread/100028?ContentTypeID=1</link><pubDate>Thu, 13 Dec 2007 06:47:06 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:6a96c79e-8bb5-4a4b-a566-19b64c44945f</guid><dc:creator>ImPer Westermark</dc:creator><description>&lt;p&gt;&lt;p&gt;
You have also forgotten (a second time) to read the information
how to post source code, despite Andy specifically notifying you
about it - and posting a link.&lt;/p&gt;

&lt;p&gt;
Now, it is time for you to pick up the documentation for rand().
It normally returns integer values between 0 and RAND_MAX, so
RAND_MAX should be a good constant to make use of.&lt;/p&gt;

&lt;p&gt;
It is quite unlikely that your RAND_MAX is 10000. A more common
value is 32767 or 65535 or any other 2^n-1 value.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: rand function</title><link>https://community.arm.com/thread/100032?ContentTypeID=1</link><pubDate>Thu, 13 Dec 2007 06:45:44 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:1c3e2677-9abf-4664-a381-1aae66e81fdc</guid><dc:creator>Christoph Franck</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;in the web, I found that 10000 should be the maximun that can
be generated&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
The maximum output of rand() depends on RAND_MAX, which should be
defined somewhere in the include files.&lt;/p&gt;

&lt;p&gt;
Also, please read and follow the instructions on how to post code,
in order to keep your code readable. They can be found above the text
window.&lt;/p&gt;

&lt;pre&gt;
int random(int min, int max){ int res = min + (int)(my_rand()/10000*(max-1)); return res;
}
&lt;/pre&gt;

&lt;p&gt;
Ok - my_rand returns an integer value between 0 and RAND_MAX,
which you then divide by 10000. This is an integer division. If
RAND_MAX is 32767, then you&amp;#39;ll only get four different values from
this division: 0, 1, 2, and 3.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: rand function</title><link>https://community.arm.com/thread/75802?ContentTypeID=1</link><pubDate>Thu, 13 Dec 2007 06:39:04 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:3d02c809-8034-4674-b18c-ddbd27e49bfb</guid><dc:creator>Christ Raoul</dc:creator><description>&lt;p&gt;&lt;p&gt;
Excuse me I have forgotten to add it&lt;br /&gt;
int my_rand () { static int first = 0; if (first == 0) { srand (43);
first = 1; } return rand (); }&lt;/p&gt;

&lt;p&gt;
int random(int min, int max){ int res = min +
(int)(my_rand()/10000*(max-1)); return res;&lt;br /&gt;
}&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: rand function</title><link>https://community.arm.com/thread/51341?ContentTypeID=1</link><pubDate>Thu, 13 Dec 2007 05:29:08 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:0c5074a7-4790-4679-a720-a3ad276257b2</guid><dc:creator>Christoph Franck</dc:creator><description>&lt;p&gt;&lt;p&gt;
What value does my_rand() return ?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: rand function</title><link>https://community.arm.com/thread/49117?ContentTypeID=1</link><pubDate>Thu, 13 Dec 2007 05:29:04 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:392abd56-91d6-4161-8bf1-e2ed50b961c3</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;&amp;quot;the below random function&amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Be sure to understand that it is a &lt;i&gt;&lt;b&gt;pseudo&lt;/b&gt;&lt;/i&gt;-random
function!&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;&amp;quot;but it didn&amp;#39;t.&amp;quot;&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
So what did it do?&lt;br /&gt;
What thought have you given to why that might be, and how it could be
changed to your requirement?&lt;/p&gt;

&lt;p&gt;
You haven&amp;#39;t shown your definition of &amp;#39;my_rand&amp;#39; - so how can anyone
tell what it might be doing?!&lt;/p&gt;

&lt;p&gt;
Please also read the instructions on how to post source code:&lt;br /&gt;
&lt;a href="http://www.danlhenry.com/caps/keil_code.png"&gt;www.danlhenry.com/.../keil_code.png&lt;/a&gt;&lt;br /&gt;

and be sure to pay attention to the preview!&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>