Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how do i choose choose a random item with different probabilities  RSS feed

 
Eric Mellen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I am creating a program in which there are strings that need to be chosen randomly. Each item has a different percent chance of being chosen say 80%, 30%, 50%, but I only want it to choose one of the items. I don't have any code because I don't really know where to start. The strings can say anything and if this doesn't sound clear just ask me something and I'll try to clarify.
 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, first you'll want to make sure you choose probabilities which add up to 1. Call them x[0], x[1], and so on.

Then choose a random number between 0 and 1.

If it's less than x[0] then your result is 0.

If it's less than x[0] + x[1] then your result is 1.

And so on.
 
Eric Mellen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you go a little farther into the last "and so on"? I'm not really getting it.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your first step is to figure out how you would do it by hand, without a computer. you can use dice to get your random number from 1-100, or 1 to 50,000 or whatever you need, but then how would you determine the outcome?
 
Eric Mellen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess I could just choose a number say 1-500 and say if x = 1 through 10 then you get a certain string, but that seems pretty inefficient.
 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Mellen wrote:Could you go a little farther into the last "and so on"? I'm not really getting it.


So make your own example. Let's say your four probabilities are 0.25, 0.30, 0.05, and 0.40 (adding up to 1). Now draw a number line going from 0 to 1 and put those probability intervals on the line, from left to right.
 
Eric Mellen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah ok that seems more clear, guess I'll try that.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric Mellen wrote:Ah ok that seems more clear, guess I'll try that.

There is a fabulous book out there called "How to Solve It" by George PĆ³lya. He was talking specifically about math problems, but the lessons can be applied to anything. I highly recommend it to everyone. He give several steps and strategies, including

1) Draw a picture
2) Solve a simpler problem first.
3) Consider special cases
4) Look for a pattern

there are more, but these are the ones I remember off the top of my head. All of these apply to many CS problems. You'll notice that this is more or less what you did.

Just some advice for the next time...
 
Eric Mellen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds really interesting, I think I'll add it to my books to read list.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!