Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. generating random number

generating random number

Scheduled Pinned Locked Moved Algorithms
tutorialquestionlounge
32 Posts 9 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    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?

    F Offline
    F Offline
    fmzl
    wrote on last edited by
    #4

    Yes I want 30% of "n" iterations the generated number will be from group A, and so on.

    FMZL

    1 Reply Last reply
    0
    • F 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

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #5

      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.

      F 1 Reply Last reply
      0
      • L Luc Pattyn

        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.

        F Offline
        F Offline
        fmzl
        wrote on last edited by
        #6

        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

        M L J 3 Replies Last reply
        0
        • F 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

          M Offline
          M Offline
          mk14882
          wrote on last edited by
          #7

          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.

          F 1 Reply Last reply
          0
          • M mk14882

            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.

            F Offline
            F Offline
            fmzl
            wrote on last edited by
            #8

            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

            1 Reply Last reply
            0
            • F 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

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #9

              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

              F 1 Reply Last reply
              0
              • L Lost User

                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

                F Offline
                F Offline
                fmzl
                wrote on last edited by
                #10

                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

                1 Reply Last reply
                0
                • F 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

                  N Offline
                  N Offline
                  NickHighIQ
                  wrote on last edited by
                  #11

                  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:

                  F 1 Reply Last reply
                  0
                  • N NickHighIQ

                    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:

                    F Offline
                    F Offline
                    fmzl
                    wrote on last edited by
                    #12

                    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

                    L N 2 Replies Last reply
                    0
                    • F 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

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #13

                      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.

                      N 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        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.

                        N Offline
                        N Offline
                        NickHighIQ
                        wrote on last edited by
                        #14

                        "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 here

                        You 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):

                        F L A 3 Replies Last reply
                        0
                        • F 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

                          N Offline
                          N Offline
                          NickHighIQ
                          wrote on last edited by
                          #15

                          Sorry mate, I replied under Luc's post - come check out the page, I've answered your question (I hope!) :)

                          1 Reply Last reply
                          0
                          • N NickHighIQ

                            "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 here

                            You 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):

                            F Offline
                            F Offline
                            fmzl
                            wrote on last edited by
                            #16

                            Thank you very much

                            FMZL

                            N 1 Reply Last reply
                            0
                            • N NickHighIQ

                              "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 here

                              You 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):

                              L Offline
                              L Offline
                              Luc Pattyn
                              wrote on last edited by
                              #17

                              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.

                              N 1 Reply Last reply
                              0
                              • F fmzl

                                Thank you very much

                                FMZL

                                N Offline
                                N Offline
                                NickHighIQ
                                wrote on last edited by
                                #18

                                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 :)

                                1 Reply Last reply
                                0
                                • L Luc Pattyn

                                  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.

                                  N Offline
                                  N Offline
                                  NickHighIQ
                                  wrote on last edited by
                                  #19

                                  I don't understand what you mean, Luc. Could you explain your comment?

                                  L 1 Reply Last reply
                                  0
                                  • N NickHighIQ

                                    I don't understand what you mean, Luc. Could you explain your comment?

                                    L Offline
                                    L Offline
                                    Luc Pattyn
                                    wrote on last edited by
                                    #20

                                    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.

                                    N 1 Reply Last reply
                                    0
                                    • L Luc Pattyn

                                      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.

                                      N Offline
                                      N Offline
                                      NickHighIQ
                                      wrote on last edited by
                                      #21

                                      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).

                                      F 1 Reply Last reply
                                      0
                                      • N NickHighIQ

                                        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).

                                        F Offline
                                        F Offline
                                        fmzl
                                        wrote on last edited by
                                        #22

                                        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

                                        P 1 Reply Last reply
                                        0
                                        • F 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

                                          P Offline
                                          P Offline
                                          PravinSingh
                                          wrote on last edited by
                                          #23

                                          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.

                                          F 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups