posted 5 years ago
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.
Eric Mellen
Greenhorn
Posts: 12
posted 5 years ago
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 1100, or 1 to 50,000 or whatever you need, but then how would you determine the outcome?
There are only two hard things in computer science: cache invalidation, naming things, and offbyone errors
Eric Mellen
Greenhorn
Posts: 12
posted 5 years ago
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 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
posted 5 years ago
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 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...
There are only two hard things in computer science: cache invalidation, naming things, and offbyone errors
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
