|
-
September 9th, 2010, 11:38 AM
#1
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..
-
September 9th, 2010, 12:26 PM
#2
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
-
September 9th, 2010, 12:41 PM
#3
Re: Is is possible to have weights in Random Range.
 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..
-
September 9th, 2010, 12:55 PM
#4
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.
-
September 9th, 2010, 01:04 PM
#5
Re: Is is possible to have weights in Random Range.
 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..
-
September 9th, 2010, 01:37 PM
#6
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.
-
September 9th, 2010, 02:36 PM
#7
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 .
-
September 9th, 2010, 03:36 PM
#8
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.
-
September 9th, 2010, 05:33 PM
#9
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..
-
September 9th, 2010, 07:28 PM
#10
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.
-
September 10th, 2010, 04:31 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|