generating random number
-
Hi, Im writing a program which has a Random object, I have 3 groups of numbers: A = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} B = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19} C = {21, 22, 23, 24, 25, 26, 27, 28, 29, 30} I put the Random object in a "for" loop which will run for n times. How I can make the Random object to generate a number which is in group A for 30% of time, in group B for 60% of time, and in group C for 10% of time. So for example if loop runs 100 times, for 30 times the generated number must be a number that exist in group A and so on. How I can do that??? Thnx
FMZL
Ok, someone has already asked this question of you, but I need some clarification. When you say "generate a number which is in group A for 30% of time", do you mean: a. Every iteration there is a 30% chance that the number will come from group A b. If I do 100 iterations, 30 of these numbers will definitely be from group A These two interpretations are very different and will require different solutions, so I need some clarification before helping you further. :cool:
-
Ok, someone has already asked this question of you, but I need some clarification. When you say "generate a number which is in group A for 30% of time", do you mean: a. Every iteration there is a 30% chance that the number will come from group A b. If I do 100 iterations, 30 of these numbers will definitely be from group A These two interpretations are very different and will require different solutions, so I need some clarification before helping you further. :cool:
I meant if I do 100 iterations, 30 of these numbers will definitely be from group A, not more than or less than 30 exactly 30, which is actually option "b", but Im interested to know the solution for option "a", I think its useful for the program im working on. Thanks
FMZL
-
I meant if I do 100 iterations, 30 of these numbers will definitely be from group A, not more than or less than 30 exactly 30, which is actually option "b", but Im interested to know the solution for option "a", I think its useful for the program im working on. Thanks
FMZL
option a is solved in my very first reply to you. option b is impossible, unless the number of runs is a multiple of 10. :doh:
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
option a is solved in my very first reply to you. option b is impossible, unless the number of runs is a multiple of 10. :doh:
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
"option b is impossible, unless the number of runs is a multiple of 10" - then it's not really impossible, is it? :laugh: @fzml Option A: Ok, for Option A, Luc's solution is what I'd go with (in fact I can't think of another way off the top of my head, at least one that's not contrived). Assume we have two sets of numbers, A and B. We want 30% chance of selecting something from A, and 70% chance of selecting something from B: in pseudo-C#, something like:
Random rnd = new Random();
int selector = rnd.Next(1, 10);
if (selector <= 3) { // select something from the A group }
else { // select something from the B group }As you can see, the rnd.Next has 10 possible outputs (1, 2, 3 ... 10) - we assign 30% of those, i.e. 1 through 3, to our 30% group. The rest make up the 70% for the other group. If we wanted 3 groups with (A: 20%, B: 50%, C: 30%):
Random rnd = new Random();
int selector = rnd.Next(1, 10);
if (selector in {1, 2}) { // select something from the A group }
else if (selector in {3, 4, 5, 6, 7}) { // select something from the B group
else { // select something from the C group }By splitting a known number of outcomes up like this, you can have "pre-determined probabilities". Of course, this all relies on the soundness of the Random generator you're using. If you would like to write your own, there's heaps of literature out there on the web about RNGs (check out the Mersenne Twister). Option B: Well, Option B (forgive me if I get the wrong terminology here) is about creating a pseudo-random number generator, with period of x, that selects members randomly from different sets based on enforced probabilities. Ok, well let's say you have two sets A and B:
A = { 1, 2, 3, 4 } // we want EXACTLY 30% of our answers to come from here
B = { 5, 6, 7, 8 } // we want EXACTLY 70% of our answers to come from hereYou need to set a "period", that is, what number of interations will satisfy the exactness of our probabilities (for example, up until now we have assumed 100 iterations will satisfay the probabilities). So, let's say we pick 100 - that means every 100 iterations, we can look at our results and say for sure that 30% of these will be from A and the rest from B. A very simple way, based on Option A, would be (pseudo-code):
-
I meant if I do 100 iterations, 30 of these numbers will definitely be from group A, not more than or less than 30 exactly 30, which is actually option "b", but Im interested to know the solution for option "a", I think its useful for the program im working on. Thanks
FMZL
Sorry mate, I replied under Luc's post - come check out the page, I've answered your question (I hope!) :)
-
"option b is impossible, unless the number of runs is a multiple of 10" - then it's not really impossible, is it? :laugh: @fzml Option A: Ok, for Option A, Luc's solution is what I'd go with (in fact I can't think of another way off the top of my head, at least one that's not contrived). Assume we have two sets of numbers, A and B. We want 30% chance of selecting something from A, and 70% chance of selecting something from B: in pseudo-C#, something like:
Random rnd = new Random();
int selector = rnd.Next(1, 10);
if (selector <= 3) { // select something from the A group }
else { // select something from the B group }As you can see, the rnd.Next has 10 possible outputs (1, 2, 3 ... 10) - we assign 30% of those, i.e. 1 through 3, to our 30% group. The rest make up the 70% for the other group. If we wanted 3 groups with (A: 20%, B: 50%, C: 30%):
Random rnd = new Random();
int selector = rnd.Next(1, 10);
if (selector in {1, 2}) { // select something from the A group }
else if (selector in {3, 4, 5, 6, 7}) { // select something from the B group
else { // select something from the C group }By splitting a known number of outcomes up like this, you can have "pre-determined probabilities". Of course, this all relies on the soundness of the Random generator you're using. If you would like to write your own, there's heaps of literature out there on the web about RNGs (check out the Mersenne Twister). Option B: Well, Option B (forgive me if I get the wrong terminology here) is about creating a pseudo-random number generator, with period of x, that selects members randomly from different sets based on enforced probabilities. Ok, well let's say you have two sets A and B:
A = { 1, 2, 3, 4 } // we want EXACTLY 30% of our answers to come from here
B = { 5, 6, 7, 8 } // we want EXACTLY 70% of our answers to come from hereYou need to set a "period", that is, what number of interations will satisfy the exactness of our probabilities (for example, up until now we have assumed 100 iterations will satisfay the probabilities). So, let's say we pick 100 - that means every 100 iterations, we can look at our results and say for sure that 30% of these will be from A and the rest from B. A very simple way, based on Option A, would be (pseudo-code):
-
"option b is impossible, unless the number of runs is a multiple of 10" - then it's not really impossible, is it? :laugh: @fzml Option A: Ok, for Option A, Luc's solution is what I'd go with (in fact I can't think of another way off the top of my head, at least one that's not contrived). Assume we have two sets of numbers, A and B. We want 30% chance of selecting something from A, and 70% chance of selecting something from B: in pseudo-C#, something like:
Random rnd = new Random();
int selector = rnd.Next(1, 10);
if (selector <= 3) { // select something from the A group }
else { // select something from the B group }As you can see, the rnd.Next has 10 possible outputs (1, 2, 3 ... 10) - we assign 30% of those, i.e. 1 through 3, to our 30% group. The rest make up the 70% for the other group. If we wanted 3 groups with (A: 20%, B: 50%, C: 30%):
Random rnd = new Random();
int selector = rnd.Next(1, 10);
if (selector in {1, 2}) { // select something from the A group }
else if (selector in {3, 4, 5, 6, 7}) { // select something from the B group
else { // select something from the C group }By splitting a known number of outcomes up like this, you can have "pre-determined probabilities". Of course, this all relies on the soundness of the Random generator you're using. If you would like to write your own, there's heaps of literature out there on the web about RNGs (check out the Mersenne Twister). Option B: Well, Option B (forgive me if I get the wrong terminology here) is about creating a pseudo-random number generator, with period of x, that selects members randomly from different sets based on enforced probabilities. Ok, well let's say you have two sets A and B:
A = { 1, 2, 3, 4 } // we want EXACTLY 30% of our answers to come from here
B = { 5, 6, 7, 8 } // we want EXACTLY 70% of our answers to come from hereYou need to set a "period", that is, what number of interations will satisfy the exactness of our probabilities (for example, up until now we have assumed 100 iterations will satisfay the probabilities). So, let's say we pick 100 - that means every 100 iterations, we can look at our results and say for sure that 30% of these will be from A and the rest from B. A very simple way, based on Option A, would be (pseudo-code):
You got lucky. :-D
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
No worries mate, you could extend option B to do this every 1000, 5000, 10000 etc. iterations. Just as long as you can divide up your selectors wholly between each group, you're home and hosed! Hope I helped, again, if you would like more explanation or an example, just email me or post again :)
-
You got lucky. :-D
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
I don't understand what you mean, Luc. Could you explain your comment?
-
I don't understand what you mean, Luc. Could you explain your comment?
you got upvoted, I got downvoted for no apparent reason. As you told him my answer was OK, he should have downvoted you too. Lots of spoons may have saved your skin. :laugh:
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
you got upvoted, I got downvoted for no apparent reason. As you told him my answer was OK, he should have downvoted you too. Lots of spoons may have saved your skin. :laugh:
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
However, remember that there's no such thing as bad questions, only bad answers, and everyone's prone to giving them. No need to be upset or insult someone when they don't understand you, simply ask how you can help to further explain yourself :) Keep in mind there's not much resolution when it comes to voting on answers, it's either "good" or "bad", no in-between. There should be another button "I don't get it", "please explain" or simply "Pauline Hanson" (Australian political reference).
-
However, remember that there's no such thing as bad questions, only bad answers, and everyone's prone to giving them. No need to be upset or insult someone when they don't understand you, simply ask how you can help to further explain yourself :) Keep in mind there's not much resolution when it comes to voting on answers, it's either "good" or "bad", no in-between. There should be another button "I don't get it", "please explain" or simply "Pauline Hanson" (Australian political reference).
Downvoting is not insulting someone, its just a way to say that this is not the solution for my problem, I think CodeProject has these two buttons ("Good Answer" and "Bad Answer")just for saying that a solution is useful or not, nothing else. so dont get offended by these things, but when you say someone sounds impolite and dumb because he just didnt vote 5 to your answer is an insult or saying "you sound dumber because of using u instead of you" is impolite and im my point of view is counted as an insult. Anyway thanks to everyone who helped me on this problem, specially NickHighIQ. Thnx
FMZL
-
Downvoting is not insulting someone, its just a way to say that this is not the solution for my problem, I think CodeProject has these two buttons ("Good Answer" and "Bad Answer")just for saying that a solution is useful or not, nothing else. so dont get offended by these things, but when you say someone sounds impolite and dumb because he just didnt vote 5 to your answer is an insult or saying "you sound dumber because of using u instead of you" is impolite and im my point of view is counted as an insult. Anyway thanks to everyone who helped me on this problem, specially NickHighIQ. Thnx
FMZL
Downvoting someone does not make anyone 'impolite and dumb'. What makes anyone that is: Not explaining what's wrong with the given answer and what's actually expected. After downvoting the answer, a 'polite and intelligent' reply like "Thanks for your efforts but what I really want is..." could have made things a lot smoother for everyone. Regarding the use of sms lingo, plz undrstnd dat dis site is read by ppl across da world, 4 many of whom English s not da 1st lang. U wud ugree dat if every1 starts using dis kinda lang then life will b hell 4 all of us. Dat's y v discourage sms lingo.
It's better to know some of the questions than all of the answers.
Pravin. -
Downvoting someone does not make anyone 'impolite and dumb'. What makes anyone that is: Not explaining what's wrong with the given answer and what's actually expected. After downvoting the answer, a 'polite and intelligent' reply like "Thanks for your efforts but what I really want is..." could have made things a lot smoother for everyone. Regarding the use of sms lingo, plz undrstnd dat dis site is read by ppl across da world, 4 many of whom English s not da 1st lang. U wud ugree dat if every1 starts using dis kinda lang then life will b hell 4 all of us. Dat's y v discourage sms lingo.
It's better to know some of the questions than all of the answers.
Pravin.can we please stop arguing about this downvoting? I recieved 100 of messages but only 2 of them are the answerws to my question. I ask a question and i recieve answers, the one that helps ME will get 5 and the one that doesn't will get 1. next time i will post my questions on msdn or somewhere else, because people here are really act like elementary school students and just looking for EXTRA MARKS. Mr.PravinSingh we are not here to give each other life lessons or advices, we are here to talk about COMPUTER PROGRAMMING, and I thought this argu was over but you like to continue it. and finally that last 2.5 lines dont make any sense to me because as i remember i just used "u" instead of "you" not any of the other words that you wrote there.
FMZL
-
can we please stop arguing about this downvoting? I recieved 100 of messages but only 2 of them are the answerws to my question. I ask a question and i recieve answers, the one that helps ME will get 5 and the one that doesn't will get 1. next time i will post my questions on msdn or somewhere else, because people here are really act like elementary school students and just looking for EXTRA MARKS. Mr.PravinSingh we are not here to give each other life lessons or advices, we are here to talk about COMPUTER PROGRAMMING, and I thought this argu was over but you like to continue it. and finally that last 2.5 lines dont make any sense to me because as i remember i just used "u" instead of "you" not any of the other words that you wrote there.
FMZL
I didn't mean to offend you. What I said was just a friendly advice on how you can avoid getting into useless discussions and what will fetch you more answers; it was "life lessons" as you call it. But since you don't want it, we should stop the discussion here. I apologize for giving you unwanted advice.:rose:
It's better to know some of the questions than all of the answers.
Pravin. -
"option b is impossible, unless the number of runs is a multiple of 10" - then it's not really impossible, is it? :laugh: @fzml Option A: Ok, for Option A, Luc's solution is what I'd go with (in fact I can't think of another way off the top of my head, at least one that's not contrived). Assume we have two sets of numbers, A and B. We want 30% chance of selecting something from A, and 70% chance of selecting something from B: in pseudo-C#, something like:
Random rnd = new Random();
int selector = rnd.Next(1, 10);
if (selector <= 3) { // select something from the A group }
else { // select something from the B group }As you can see, the rnd.Next has 10 possible outputs (1, 2, 3 ... 10) - we assign 30% of those, i.e. 1 through 3, to our 30% group. The rest make up the 70% for the other group. If we wanted 3 groups with (A: 20%, B: 50%, C: 30%):
Random rnd = new Random();
int selector = rnd.Next(1, 10);
if (selector in {1, 2}) { // select something from the A group }
else if (selector in {3, 4, 5, 6, 7}) { // select something from the B group
else { // select something from the C group }By splitting a known number of outcomes up like this, you can have "pre-determined probabilities". Of course, this all relies on the soundness of the Random generator you're using. If you would like to write your own, there's heaps of literature out there on the web about RNGs (check out the Mersenne Twister). Option B: Well, Option B (forgive me if I get the wrong terminology here) is about creating a pseudo-random number generator, with period of x, that selects members randomly from different sets based on enforced probabilities. Ok, well let's say you have two sets A and B:
A = { 1, 2, 3, 4 } // we want EXACTLY 30% of our answers to come from here
B = { 5, 6, 7, 8 } // we want EXACTLY 70% of our answers to come from hereYou need to set a "period", that is, what number of interations will satisfy the exactness of our probabilities (for example, up until now we have assumed 100 iterations will satisfay the probabilities). So, let's say we pick 100 - that means every 100 iterations, we can look at our results and say for sure that 30% of these will be from A and the rest from B. A very simple way, based on Option A, would be (pseudo-code):
How did you make those green code blocks with each line numbered?
-
Hi, Im writing a program which has a Random object, I have 3 groups of numbers: A = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} B = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19} C = {21, 22, 23, 24, 25, 26, 27, 28, 29, 30} I put the Random object in a "for" loop which will run for n times. How I can make the Random object to generate a number which is in group A for 30% of time, in group B for 60% of time, and in group C for 10% of time. So for example if loop runs 100 times, for 30 times the generated number must be a number that exist in group A and so on. How I can do that??? Thnx
FMZL
-
well, do u expect me to vote 5 for ur answer?? I think u r impolite and too much proud, Ive stated in my question that I want to use a Random object, I didnt ask u to suggest me an alternative, sorry plz first read the question carefully and if it still sounds nonsense to u, ask for more explanation, someone else did that and i explained. And as I read ur answer u didnt say anything about that I didnt explain properly, so we can think u understood the problem completely, and this was ur answer which cannot help me to solve this problem, anyway if it really makes u happy, ill give u 5. good luck.
FMZL
fmzl wrote:
well, do u expect me to vote 5 for ur answer?? I think u r impolite
I've read his posts and don't see a single impolite thing. Good luck with your approach - you'll need it. You might try reviewing your well worn copy of Dale Carnegie.
-
fmzl wrote:
well, do u expect me to vote 5 for ur answer?? I think u r impolite
I've read his posts and don't see a single impolite thing. Good luck with your approach - you'll need it. You might try reviewing your well worn copy of Dale Carnegie.
-
If you have a specific question, ask it. Otherwise, I'm sure you can figure it out or Google it.