<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[[SOLVED] How to get random number in a range]]></title><description><![CDATA[[[topic:post_is_deleted]]]]></description><link>https://forum.pycom.io/topic/1378/solved-how-to-get-random-number-in-a-range</link><generator>RSS for Node</generator><lastBuildDate>Wed, 22 Apr 2026 17:17:35 GMT</lastBuildDate><atom:link href="https://forum.pycom.io/topic/1378.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 19 Jun 2017 13:34:06 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [SOLVED] How to get random number in a range on Mon, 19 Jun 2017 13:55:35 GMT]]></title><description><![CDATA[<p dir="auto">You should look at urandom. It gives you a random number within the byte size you pass into the function. This gives you a good value to scale with. I cannot remember how &quot;good&quot; this is for random numbers, especially for cryptography, but probably good enough for many cases.</p>
<p dir="auto">You could do something simple, don't judge, quickly typing this without testing and to prove a point. This is not best practice.</p>
<p dir="auto">the_range = 41-40<br />
result = uos.urandom(1) / 256 * the_range</p>
<p dir="auto"><a href="https://docs.pycom.io/pycom_esp32/library/uos.html#uos.urandom" target="_blank" rel="noopener noreferrer nofollow">https://docs.pycom.io/pycom_esp32/library/uos.html#uos.urandom</a></p>
]]></description><link>https://forum.pycom.io/post/8628</link><guid isPermaLink="true">https://forum.pycom.io/post/8628</guid><dc:creator><![CDATA[TravisT]]></dc:creator><pubDate>Mon, 19 Jun 2017 13:55:35 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] How to get random number in a range on Mon, 19 Jun 2017 15:03:47 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/583">@TravisT</a> said in <a href="/post/8628">How to get random number in a range</a>:</p>
<blockquote>
<p dir="auto">result = uos.urandom(1) / 256 * the_range</p>
</blockquote>
<p dir="auto">Thanks for your reply, it gives me this error:</p>
<p dir="auto">TypeError: unsupported types for <strong>truediv</strong>: 'bytes', 'int'</p>
]]></description><link>https://forum.pycom.io/post/8640</link><guid isPermaLink="true">https://forum.pycom.io/post/8640</guid><dc:creator><![CDATA[amartinez]]></dc:creator><pubDate>Mon, 19 Jun 2017 15:03:47 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] How to get random number in a range on Mon, 19 Jun 2017 18:20:28 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/1318">@amartinez</a>  you could code as follows:</p>
<pre><code>import  uos
result = (uos.urandom(1)[0] / 256) + 40
</code></pre>
<p dir="auto">Which will give you a number between 40.0 and 40.99609375. Since you started with a byte, there are only 256 different values.</p>
]]></description><link>https://forum.pycom.io/post/8641</link><guid isPermaLink="true">https://forum.pycom.io/post/8641</guid><dc:creator><![CDATA[robert-hh]]></dc:creator><pubDate>Mon, 19 Jun 2017 18:20:28 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] How to get random number in a range on Mon, 19 Jun 2017 17:42:02 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/1318">@amartinez</a><br />
based on my old post<br />
<a href="https://forum.pycom.io/topic/1110/random-number-generator/2">https://forum.pycom.io/topic/1110/random-number-generator/2</a></p>
<pre><code>def Random():
   r = crypto.getrandbits(32)
   return ((r[0]&lt;&lt;24)+(r[1]&lt;&lt;16)+(r[2]&lt;&lt;8)+r[3])/4294967295.0

def RandomRange(rfrom, rto):
   return Random()*(rto-rfrom)+rfrom
</code></pre>
]]></description><link>https://forum.pycom.io/post/8642</link><guid isPermaLink="true">https://forum.pycom.io/post/8642</guid><dc:creator><![CDATA[livius]]></dc:creator><pubDate>Mon, 19 Jun 2017 17:42:02 GMT</pubDate></item><item><title><![CDATA[Reply to [SOLVED] How to get random number in a range on Mon, 19 Jun 2017 18:52:25 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/98">@robert-hh</a> <a class="plugin-mentions-user plugin-mentions-a" href="https://forum.pycom.io/uid/199">@livius</a> thank you so much!</p>
]]></description><link>https://forum.pycom.io/post/8650</link><guid isPermaLink="true">https://forum.pycom.io/post/8650</guid><dc:creator><![CDATA[amartinez]]></dc:creator><pubDate>Mon, 19 Jun 2017 18:52:25 GMT</pubDate></item></channel></rss>