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.
  • 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
                        • P PravinSingh

                          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 Offline
                          F Offline
                          fmzl
                          wrote on last edited by
                          #24

                          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

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

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

                            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.

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

                              A Offline
                              A Offline
                              AspDotNetDev
                              wrote on last edited by
                              #26

                              How did you make those green code blocks with each line numbered?

                              [Forum Guidelines]

                              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

                                T Offline
                                T Offline
                                T2102
                                wrote on last edited by
                                #27

                                This is the first random number generation method they usually teach in an introductory simulation course. Think it's called Aliasing or something like that.

                                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

                                  J Offline
                                  J Offline
                                  Jeff Connelly
                                  wrote on last edited by
                                  #28

                                  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.

                                  F 1 Reply Last reply
                                  0
                                  • J Jeff Connelly

                                    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.

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

                                    ??????

                                    FMZL

                                    J 1 Reply Last reply
                                    0
                                    • F fmzl

                                      ??????

                                      FMZL

                                      J Offline
                                      J Offline
                                      Jeff Connelly
                                      wrote on last edited by
                                      #30

                                      If you have a specific question, ask it. Otherwise, I'm sure you can figure it out or Google it.

                                      F 1 Reply Last reply
                                      0
                                      • J Jeff Connelly

                                        If you have a specific question, ask it. Otherwise, I'm sure you can figure it out or Google it.

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

                                        i just didnt get your previous message: 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 what do you mean?

                                        FMZL

                                        J 1 Reply Last reply
                                        0
                                        • F fmzl

                                          i just didnt get your previous message: 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 what do you mean?

                                          FMZL

                                          J Offline
                                          J Offline
                                          Jeff Connelly
                                          wrote on last edited by
                                          #32

                                          As I said, you could Google it. Dale Carnegie is famous for a book called "How to Win Friends and Influence People".

                                          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