random number with probability

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maniacCow
    New Member
    • Aug 2010
    • 24

    random number with probability

    Is there any way to make a number that generated by random number is the highest probability among all?

    eg.

    random no 1 20%
    random no 2 (Higher percentage) - 60%
    random no 3 20%
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    I don't know of any built in way, but you could always do something like...

    Code:
    int randVal = Rand.Next(10);
    int retVal = 0;
    switch (randVal)
    {
      case 0:
      case 1:
        retVal = 1;
        break;
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
        retVal = 2;
        break;
      case 8:
      case 9:
        retVal = 3;
        break;
    }
    
    return retVal;
    You'd have to extend that for other numbers :)

    Comment

    • maniacCow
      New Member
      • Aug 2010
      • 24

      #3
      Great. Can be used too. I have a try first den I let u know my result. Thx bro ;)

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        I'm not a c# kind of guy, but the code @garyTexmo wrote is a little bit difficult to implement for the general case.

        In my example, I assume that the method/function Rand() returns a floating point value in the range of zero to one, with even distibution.
        Code:
        float rValue = Rand();   // should be in the range 0.->1.0
        
        int iValue;
        if (rValue < 0.20)       iValue = 1;
        else if (rValue < 0.80)  iValue = 2;
        else                     iValue = 3;
        Hopefully the methodology makes sense - the bound on each subsequent if statement is the sum of all probabilities up to that point.

        Cheers!

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          I agree with you, Oralloy, I just wrote it like that to demonstrate the idea of separating the ranges. Your way is definitely more elegant. Though I would advocate the switch and the integer only if you're trying to squeeze as much speed as possible out of your app, which these days is a fairly rare case.

          In my head I was envisioning a class where you registered values with probabilities and it would return a value based on that, haha.

          Comment

          • Oralloy
            Recognized Expert Contributor
            • Jun 2010
            • 988

            #6
            @GaryTexmo

            There's no problem with having a class to encapsulate the idea of a non-uniformly distributed random number generator.

            The trick is in the implementation. There are some fairly well developed transformations for things like gaussian distributions. But for an arbitrary distribution, it's all fair game.

            Get the generator to work right first, then optimize it.

            As a side note, optimization is a tricky thing.

            Who was it that said 97% of all optimization isn't?

            Quips aside, the structure used to implement should reflect the distribution of the data and the value range. If there are 1,000 possibilities, then I'd say we should consider a binary search into a lookup vector. If we were to reproduce a continuum, then I'd say we have to be a little more clever.

            However, for three items, I don't see how we could get any smarter than cascading ifs.

            If the values were highly skewed to a very few, then a combination of the above might be best. Or maybe a depth optimized look-up tree.

            On the other hand your method is good, if the dispatch down that switch was only one instruction/memory fetch. I think the VAX architecture allowed dispatches on a fixed range of integer values of up to 256 elements in one instruction cycle. However I'd hate to try a similar beast on an IBM360.

            Oh well, enough mental play - time to get back to work and earn my paycheck.

            Comment

            • GaryTexmo
              Recognized Expert Top Contributor
              • Jul 2009
              • 1501

              #7
              Statistics was never my thing, a lot of what you said is above me!

              :D

              Comment

              • Oralloy
                Recognized Expert Contributor
                • Jun 2010
                • 988

                #8
                Sorry, @GaryTexmo, I didn't mean to get on my academic horse.

                Basically I wrote that building a good non-linear random number generator is non-trivial.

                Optimizing one is worse.

                Cheers!

                Comment

                • maniacCow
                  New Member
                  • Aug 2010
                  • 24

                  #9
                  Hmmm... I figuring out another way. But seem like couldn't get the result...

                  Code:
                  using System;
                  using System.Collections.Generic;
                  using System.Linq;
                  using System.Text;
                  
                  namespace ConsoleApplication4
                  {
                      class Program
                      {
                          class mydatatype
                          {
                              public string foodname;
                              public int quantity;
                          }
                  
                          public Program() { }
                  
                          static void Main(string[] args)
                          {
                              List<mydatatype> mylist = new List<mydatatype>();
                  
                              string fdname1, fdname2;
                  
                              Random rd = new Random();
                              mydatatype x = new mydatatype();
                  
                              for (int i = 0; i < 30; i++)
                              {
                  
                                  int randome = rd.Next(1, 11);
                  
                                  if (randome <= 8)
                                  {
                                      fdname1 = "kampong";
                                      x.foodname = fdname1;
                                      x.quantity = 1;
                                  }
                                  else
                                  {
                                      fdname2 = "chinese";
                                      x.foodname = fdname2;
                                      x.quantity = 1;
                                  }
                                  mylist.Add(x);
                              }
                  
                              foreach(mydatatype m1 in mylist)
                              {
                                  System.Console.WriteLine("Qty:" + m1.quantity + "\tFdname:" + m1.foodname.ToString());
                              }
                          }
                      }
                  }

                  Comment

                  • Oralloy
                    Recognized Expert Contributor
                    • Jun 2010
                    • 988

                    #10
                    Let me guess, you get the same answer for all foods?

                    It looks like you only have one instance of the variable x, which you refer to in all 30 elements of your list.

                    Move the definition/assignment
                    Code:
                    mydatatype x = new mydatatype();
                    Inside your process loop.

                    Cheers!
                    Last edited by Oralloy; Aug 11 '10, 04:27 PM. Reason: Removed invalid statistical comment.

                    Comment

                    • maniacCow
                      New Member
                      • Aug 2010
                      • 24

                      #11
                      Thx @Oralloy
                      You save me ;)

                      Comment

                      • GaryTexmo
                        Recognized Expert Top Contributor
                        • Jul 2009
                        • 1501

                        #12
                        Oh, no offense was taken and feel free to get on your academic high horse as much as you want! I'm just saying you went above me haha :P

                        Comment

                        • Oralloy
                          Recognized Expert Contributor
                          • Jun 2010
                          • 988

                          #13
                          Does that mean that I can enter academic steeplechase competitions, now?

                          :))

                          Comment

                          Working...