Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: inclusive rand

by BrowserUk (Patriarch)
on Mar 13, 2017 at 13:48 UTC ( [id://1184422]=note: print w/replies, xml ) Need Help??


in reply to inclusive rand

If you multiply rand by 101, int it, then divide by 100, you'll get all possible (2-decimal place) values between 0 and 1 inclusive with 'equal probability':

If you need 3 decimal places, use 1000*int( rand *1001 ); and for 4: 10000*int( rand * 10001 ) etc. Corrected. Thanks to pryrt.

If you need 3 decimal places, use int( rand *1001 )/1000; and for 4: int( rand * 10001 )/10000 etc.

(But beware the number of bits available in the rand() you use. On Windows, pre-5.20something, perl's rand() only has 15-bits. )


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: inclusive rand
by pryrt (Abbot) on Mar 13, 2017 at 15:31 UTC

    "... use 1000*int( rand *1001 ); and for 4: 10000*int( rand * 10001 ) etc." doesn't match the first paragraph, where you multiply by the bigger and divide by the smaller; here, you multiplied twice. I believe it should be, "... use int( rand *1001 )/1000; and for 4: int( rand * 10001 )/10000 etc."?

      Tested code rules over untestable text every time :) I'll correct it. Thanks.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: inclusive rand
by msh210 (Monk) on Mar 13, 2017 at 14:24 UTC

    Thanks. That's roughly what others have answered.

    $_="msh210";$"=$\;@_=@{[split//,uc]}[2,0];$_="@_$\1";$\=$/;++$_[0]for$...1;print lc substr crypt($_,"@_"),1,6

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1184422]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 13:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found