Note that there are some explanatory texts on larger screens.

plurals
  1. POJavaScript for Random Numbers with Recursion
    primarykey
    data
    text
    <p>I'm trying to create a javascript function that accepts 2 parameters <code>min</code> and <code>max</code> and generates a random number between the two integers. That part is easy.</p> <p>Where things get rough is that I need to create a conditional that says </p> <pre><code>function generateNum (min, max) { var randNumber = Math.ceil(Math.random()*(max - min)+min); if (randNumber === max) { // store the result (probably in an array) and generate a new // number with the same behavior as randNumber (e.g. it is also // stores it's total in the array and recursively "re-generates // a new number until max is not hit) } } </code></pre> <p>The idea is to recursive-ise this so that a running total of the number of max hits is stored, combined, and then returned.</p> <p>For example: The script receives min / max parameters of 5/10 <code>generateNum(5,10){}</code>. If the value generated by <code>randNumber</code> were 5, 6, 7, 8, or 9 then there would be no recursion and the function would return that value. If the value generated by <code>randNumber</code> is <code>10</code>, then the value <code>10</code> is stored in an array and the function now "re-tries" recursively (meaning that as many times as 10 is generated, then that value is stored as an additional object in the array and the function re-tries). When the process stops (which could be infinite but has a parabolically decreasing probability of repeating with each recursion). The final number (5, 6, 7, 8, 9) would be added to the total of generated <code>max</code> values and the result would be returned.</p> <p>Quite an unusual mathematic scenario, let me know how I can clarify if that doesn't make sense.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload