random selection of array elements

Started by
27 comments, last by heh65532 6 years, 7 months ago
6 minutes ago, Kylotan said:

The code wrongly uses the relative frequencies in the array rather than the cumulative frequencies, so that it's possible to generate a random value that will never be less than or equal to any of the values in the array.

And the iteration should be forwards, not backwards. Currently you'd exit the loop on the first iteration every time (if the previous bug was fixed).

Once those are fixed, this would be a reasonable implementation of roulette wheel selection.

 

The code uses relative frequencies (1000, 1000, 1000, 3000). It would be more clear if he actually wrote that and then some code that did the accumulation, or if he had stuck to the original example so we know for sure what he means.

The iteration seems fine to me.

 

Advertisement

Okay, given that interpretation I can see how it would 'work', even if it is incomprehensible.

How do you do black background code snippets?

I ran to a problem with the latest code  I posted. While arrays like this work well:


int array[asize] = {0, 1000, 2000, 3000};

 

it doesn't work anymore if the values are closer to each other like this:

 


int array[asize] = {0, 1000, 2000, 2001};

The 2001 has only small change of being picked while it should have about same change of being picked than the 2000.

 

So not sure how to handle that.

Someone please correct me if I'm wrong or if this is a terrible idea. It seemed to work in excel. Largest number gets 50% chance and each following number gets half the chance of the previous. The actual numbers in the array are not important. You would reach a limit to how many potential numbers can be selected quite quickly though.


Count the number of items in your list.
MaxRandValue = 2^arrayElementCount
ra = rand()%MaxRandValue
index = log10(ra)/log10(2)
return array[index]

 

As mentioned a few times now, problems like this are easy to solve, but it requires a firm definition of the actual requirements. Until we see those, everything else is just shooting in the dark.

15 hours ago, heh65532 said:

I ran to a problem with the latest code  I posted. While arrays like this work well:



int array[asize] = {0, 1000, 2000, 3000};

 

it doesn't work anymore if the values are closer to each other like this:

 



int array[asize] = {0, 1000, 2000, 2001};

The 2001 has only small change of being picked while it should have about same change of being picked than the 2000.

 

So not sure how to handle that.

 

Please re-read my post about this method. Notice how I turned 1000, 800, 600, 400 in your example into the appropriate array values. When I use the phrase "partial sums", I mean that the array should contain the cumulative probability of all previous results. So if array[3]-array[2] is 1, you are saying that the probability of getting a 2 should be proportional to 1, and therefore tiny.

 

1 hour ago, alvaro said:

 

Please re-read my post about this method. Notice how I turned 1000, 800, 600, 400 in your example into the appropriate array values. When I use the phrase "partial sums", I mean that the array should contain the cumulative probability of all previous results. So if array[3]-array[2] is 1, you are saying that the probability of getting a 2 should be proportional to 1, and therefore tiny.

 

Oops I missed what you said there.. Got it working now!

thanks!

This topic is closed to new replies.

Advertisement