The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Game Development
|
Function which returns true with a given probability
Discuss Function which returns true with a given probability in the Game Development forum on Dev Shed. Function which returns true with a given probability Game Development forum covering non language specific programming - game creation, design, modding, theories and math. A place for developers and gamers of all levels to discuss and debate all things involved in game creation and modding.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

April 22nd, 2007, 03:57 PM
|
|
Registered User
|
|
Join Date: Apr 2007
Posts: 3
Time spent in forums: 29 m 14 sec
Reputation Power: 0
|
|
|
Function which returns true with a given probability
I need to create a function which returns true with a given probability. For example:
foo(.1) returns true with probability .1
Could someone provide either a ruby implementation or a generic way to do this?
|

April 24th, 2007, 08:23 AM
|
|
rebel with a cause
|
|
Join Date: May 2004
Location: The Batsh!t Crazy State.
|
|
Code:
public boolean foo(double probability) {
return true;
}
Seriously, You've got to specify some more logical requirements than that.
__________________
Dear God. What is it like in your funny little brains? It must be so boring.
|

April 26th, 2007, 02:47 AM
|
|
Registered User
|
|
Join Date: Apr 2007
Posts: 3
Time spent in forums: 29 m 14 sec
Reputation Power: 0
|
|
|
I've said plenty. The technical term for what I have described is weighted random selection. In this case, the selection is between two elements: true and false. The probability p, given as an argument, is the weight assigned to true, and therefore the weight assigned to false is 1-p.
I ended up finding a Ruby implementation of weighted random selection that works for any number of elements; using this, I wrote my function.
|

April 26th, 2007, 11:13 AM
|
 |
Kage Bunshin
|
|
Join Date: Aug 2005
Location: The Seven Seas Of Rhye
|
|
|
Its one of those things you have to read twice. Trust me, he isn't usually so "out-of-it". Also, welcome to Dev Shed =D
__________________
"Java makes impossible things possible, but makes easy things difficult." - Somebody
|

April 26th, 2007, 04:18 PM
|
|
Registered User
|
|
Join Date: Apr 2007
Posts: 3
Time spent in forums: 29 m 14 sec
Reputation Power: 0
|
|
Quote: | Its one of those things you have to read twice. |
Agreed. Also, I probably could have done better job explaining what I was looking for.

|

April 26th, 2007, 05:58 PM
|
|
|
|
Use a PRNG.
Say you take a probability out of 100. So for foo(.1), that's really like saying, I have a random number from 1-100. Is it less than 10? A standard PRNG available in most languages gives you a uniform distribution, so the probability is .1 that the number is less than 10. So look up creating random numbers in Ruby.
Generate random number within a range (something like 1-100, 1-1000). Check if random number is less than probability*range. If it is, return true, otherwise return false.
__________________
When you ask a question, be prepared to tell us: what have you tried? If you think you don't need to try anything, we will never be interested in helping you. If you agree with the link, and you refuse to answer that question, you are being a hypocrite.
Need help with broken code? Your question should be like a good bug report: (1) It has the smallest number of steps to reproduce the problem you see (2) It tells us precisely what you expected to see and (3) It tells us what you saw and how it differed from what you expected. We need all three to help you.
Want better answers? Tell us what you Googled for and what steps you took to answer your own question.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|