JavaScript (ES6), 40 bytes
f=n=>alert(n=Math.random()*n|0)||n&&f(n) // Alert is replaced with console.log to avoid the popups alert=console.log f=n=>alert(n=Math.random()*n|0)||n&&f(n) <input type=number id=a><button onclick="f(a.value)">Test</button>