language agnostic - What's the correct way to expand a [0,1] interval to [a,b]? -


Many random number generators return floating numbers between 0 and 1.

What is the best and correct way a and b

integer between itemprop, for example a = 2, b = 5

  [- --- + ---- + ---- + ----] 0 1/4 1/2 3/4 1 Modes to 2 3 4 5  

Formula with problem

  Int ( Rnd () * (B-A + 1)) + A  

It is that your Rnd () generation interval is on both sides, thus both 0 and 1 are possible outputs And Formula 6 returns when Rand (1) is exactly 1.

In a genuine random distribution (no pseudo), the probability of 1 is zero I think it is quite safe for the program like:

  r = Rnd () If r is equal to 1 MyInt = B other MyInt = Int (r * (BA + 1)) + A Endif  

Edit

Just one quick test:

Set our work:

  f [a_, b_]: = if [(R = random real []) == 1, B, create a table with 10 ^ 5 numbers in [10]: [/ b] C Ode> table = sort [match [table [f [1, 100], {300000}], first  
 in  [137]: = {max / @ Table], check out the minimum [first / @ table]} [137] = {100, 1}  

lets see the distribution:

  Sort bar [last / @ [matching [table [f [1, 100], {300000}], first], chartstyle - & gt; "DarkChinbo"]  

alt text


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -