Game Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesGame Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old April 22nd, 2007, 03:57 PM
techwiz18 techwiz18 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 3 techwiz18 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old April 24th, 2007, 08:23 AM
crownjewel82's Avatar
crownjewel82 crownjewel82 is offline
rebel with a cause
Dev Shed God (5000 - 5499 posts)
 
Join Date: May 2004
Location: The Batsh!t Crazy State.
Posts: 5,340 crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)crownjewel82 User rank is General 14th Grade (Above 100000 Reputation Level)  Folding Points: 31009 Folding Title: Starter FolderFolding Points: 31009 Folding Title: Starter Folder
Time spent in forums: 2 Months 3 Weeks 4 Days 3 h 56 m 38 sec
Reputation Power: 1920
Code:
public boolean foo(double probability) {
    return true;
}


Seriously, You've got to specify some more logical requirements than that.
__________________
The day I get my hands on the cookbook it's all over. -nicky

Reply With Quote
  #3  
Old April 26th, 2007, 02:47 AM
techwiz18 techwiz18 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 3 techwiz18 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old April 26th, 2007, 11:13 AM
tagmanadvance's Avatar
tagmanadvance tagmanadvance is offline
Kage Bunshin
Dev Shed Novice (500 - 999 posts)
 
Join Date: Aug 2005
Location: The Seven Seas Of Rhye
Posts: 752 tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level)tagmanadvance User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 22 h 58 m
Reputation Power: 184
Send a message via AIM to tagmanadvance Send a message via Yahoo to tagmanadvance Send a message via XFire to tagmanadvance
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

Reply With Quote
  #5  
Old April 26th, 2007, 04:18 PM
techwiz18 techwiz18 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 3 techwiz18 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.


Reply With Quote
  #6  
Old April 26th, 2007, 05:58 PM
Oler1s Oler1s is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2006
Posts: 1,460 Oler1s User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Oler1s User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Oler1s User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Oler1s User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Oler1s User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Oler1s User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Oler1s User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Oler1s User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Oler1s User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Oler1s User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Oler1s User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 4 Weeks 6 h 41 m 21 sec
Reputation Power: 425
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.
__________________
C/C++: Books, C FAQ, C++ FAQ, C/C++ Reference.
Python: Website, Beautiful Soup.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesGame Development > Function which returns true with a given probability


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway