CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 2010
    Posts
    26

    Is is possible to have weights in Random Range.

    I've searched the internet, I've seen weights used in an array list or enum but i havent seen it used in a random range.

    I mean if I'm using Random

    Code:
     Random.Next(Min, Max);
    method. and i have a min random number range of 3, and max random number range of 5.

    I want to use weights that add up to 1.00
    Range number 3 = .50 or 50%
    Range number 4 = .45 etc
    Range number 5 = .05 etc

    I guess my question is pragmatic meaning that what is the logic on weights on how do you trick the program to make it think that one number weight more then the other so when it does randomly picks a number it using the weights...
    Any help will be greatly appreciated..

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Is is possible to have weights in Random Range.

    A Rnd() number is between 0 and 1, so just multiply by 5, and use the INT().

    That gives 0-4. Add +1 for 1-5
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Aug 2010
    Posts
    26

    Re: Is is possible to have weights in Random Range.

    Quote Originally Posted by dglienna
    A Rnd() number is between 0 and 1, so just multiply by 5, and use the INT().

    That gives 0-4. Add +1 for 1-5
    Sorry but i'm confuse about what you are saying here. I don't know why i'm muliplying by 5. i'm just trying to add weights to the range. that way the probability of the random number choosing one random number over the other is greater then the others..

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Is is possible to have weights in Random Range.

    Well if I understand you correctly one way would be to use an array of source numbers and pick an element from that array at random. In this array you would have some numbers in more places than others which would effectively cause that number to have a higher likely hood of showing up in the result.

    Lets say you have 10 elements in your array
    1,2,3,4,2,3,4,2,3,2

    If you pick a random number between 0 and 9 and grab that element from the array then the odds would be 1 in 10 of getting a 1 but 4 in 10 of getting a 2

    Is this what you are asking?
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Aug 2010
    Posts
    26

    Re: Is is possible to have weights in Random Range.

    Quote Originally Posted by DataMiser
    Well if I understand you correctly one way would be to use an array of source numbers and pick an element from that array at random. In this array you would have some numbers in more places than others which would effectively cause that number to have a higher likely hood of showing up in the result.

    Lets say you have 10 elements in your array
    1,2,3,4,2,3,4,2,3,2
    DataMiser that is one way of looking at it but i would be putting the number 3, 4, 5 in the array. I've seen code where people use weights instead of array. I'm assuming this would work about the same way. Wouldnt it ? the end goal is to use some type of distribution model weight where the likely hood of some one random number gets choose higher then other numbers..

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Is is possible to have weights in Random Range.

    I doesn't matter what numbers you would be putting into it just that they occur at different frequencies and one of them is picked at random. If 3 occurs twice as many times in the array as 4 then 3 is twice as likely to be the random selection.
    Always use [code][/code] tags when posting code.

  7. #7
    Join Date
    Aug 2010
    Posts
    26

    Re: Is is possible to have weights in Random Range.

    Dataminer,

    When trying to use this code. Its not getting the numbers that i populated inside the array. How would i pull one of the numbers from the array.

    Code:
       int[] numbers = new int[] {4, 5, 3, 5, 4, 4, 5, 5, 4, 4, 3, 5, 
                    5, 4, 5, 3, 4, 5, 4, 5, 4, 5, 4, 5, 4, 5, 5, 4};
                
                int numberOfElementsToRemove = listRand.Next(numbers[27]);
    I didn't see which method to use to pull one of the numbers out of by .

  8. #8
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Is is possible to have weights in Random Range.

    c# is not my native language so I am not clear on all functions and syntax.

    You should however be able to simply create an int, assign it a random number as needed and grab that element from the array

    Not sure what you are trying to do with number of elements to remove.

    You would set your result = to numbers[RandomValue];

    So if your random number was 3 it would retrieve the 4th element from the array or whatever the case may be
    Always use [code][/code] tags when posting code.

  9. #9
    Join Date
    Jan 2007
    Posts
    491

    Re: Is is possible to have weights in Random Range.

    If you want some specific case, the code above is just fine for you.
    if you want to support something a little more generic, here's a way to do that without using an array:
    Code:
    class Rational
    {
        private int n, int d;
        public int Numerator {get {return n;}}
        public int Denominator {get {return d;}}
    
        public Rational(int n, int d)
        {
            this.n = n;
            this.d = d;
        }
    
        public static Rational operator / (int n, int d)
        {
             return new Rational(n, d);
        }
    }
    
    class WeightedNumber
    {
         public Rational Weight {get; set;}
         public int Number {get; set;}
    }
    
    private int GCD(int x, int y) //greatest common divisor
    {
         if(y == 0)
            return x;
         return GCD(y, x % y);
    }
    
    private int LCM(int x, int y) //least common multiple
    {
        return x * y / GCD(x, y);
    }
    
    private int GenerateWeightedNumber(List<WeightedNumber> numbers)
    {
        Random rnd = new Random(DateTime.Now.MilliSecond);
        int lcm = numbers.Aggregate(1, (res, x) => LCM(res, x.Weight.Denominator));
        var props = from n in numbers
                    select n.Weight.Numerator * (lcm / n.Weight.Denominator);
        int current = 0, i = 0, res = rnd.Next(0, lcm) + 1;
        while(current + props[i] < res)
        {
            current += props[i];
            i++;
        }
        return numbers[i].Number;
    }
    didn't test it, but hope you get the idea..

  10. #10
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Is is possible to have weights in Random Range.

    You're using res before you give it a value, and don't use RND.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  11. #11
    Join Date
    Jan 2007
    Posts
    491

    Re: Is is possible to have weights in Random Range.

    what? I can't see anything wrong with the code.
    Code:
        Random rnd = new Random(DateTime.Now.MilliSecond
        ...
        int current = 0, i = 0, res = rnd.Next(0, lcm) + 1;
        while(current + props[i] < res)
        {
              ...
         }
         ...
    plus, as i mentioned before, there might be such mistakes there. i didn't test it, didn't even checked if it compiles successfully. the essence of the code is to show the idea behind it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured