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
-
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
-
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
you could create one collection (array, list) containing all the elements of A three times, those of B six times, those of C once, for a total of 100 numbers; order does not matter. Then run a random number generator that returns a number in the range [0,99] and use that as the index in your collection to fetch the next output value of your Random object. BTW: what happened to 20? The above gives the concept, you don't have to actually create the overall collection, you could as well manipulate the index a bit like this (pseudo-code):
int next=random.Next(0,100);
int letter=next/10;
int index=next%10;
if (letter in {0,1,2}) return A[index];
else if (letter in {3 to 8} return B[index];
else return C[index];:)
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.
-
Confusing. Do you want a 30% chance that it will be from group A, or do you want that every 100 iterations exactly 30 numbers were chosen from group A?
-
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
If and when you receive a reply you don't like for some reason, e.g. because it is not solving the problem you have, chances are you did not explain properly. Simply downvoting the answer without explaining yourself is not only impolite but also dumb, as it does not help you in getting a more fitting answer. Good luck. :^)
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.
-
If and when you receive a reply you don't like for some reason, e.g. because it is not solving the problem you have, chances are you did not explain properly. Simply downvoting the answer without explaining yourself is not only impolite but also dumb, as it does not help you in getting a more fitting answer. Good luck. :^)
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.
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
-
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
I don't think Luc is expecting 5 but a reason for giving him 1. As a basic level programmer, I feel his answer is near to your question and as you know people here should build a programming community to share all of our knowledge instead of fighting. Please let us know what you have tried. We will also learn.
-
I don't think Luc is expecting 5 but a reason for giving him 1. As a basic level programmer, I feel his answer is near to your question and as you know people here should build a programming community to share all of our knowledge instead of fighting. Please let us know what you have tried. We will also learn.
he used the words impolite and dumb for me, i didnt start a fight, he could ask a me for more explanation, but as i said he just wrote his answer and didnt mention that he didnt understand my question properly, if anyone asks me for more explanation i will do it. bye
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
With an attitude like this you are not likely to get much help from this community. Whatever you think of Luc's answer he is giving up his own time at no cost to you to try and help you solve your problems. The very least you could do is to accept it with good grace. Also using txtspk: words like u, ur, plz instead of you, your, please, you sound even dumber.
Just say 'NO' to evaluated arguments for diadic functions! Ash
-
With an attitude like this you are not likely to get much help from this community. Whatever you think of Luc's answer he is giving up his own time at no cost to you to try and help you solve your problems. The very least you could do is to accept it with good grace. Also using txtspk: words like u, ur, plz instead of you, your, please, you sound even dumber.
Just say 'NO' to evaluated arguments for diadic functions! Ash
as YOU wish, using "u" makes someone sounds dumb but just writing something as answer to a question without reading and understaing the question is not dumb, I dont know what is wrong YOU people, and as I remember I didnt beg anyone to give up on his/her time for my answering my question, its up YOU to answer it. and if it really bothers YOU that I dont vote 5 to a nonsense, YOU can remove my question from the forum, this a thing that YOU guys are very good at here, vote for removing someones article or ... .
FMZL
-
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.