tags:

views:

233

answers:

5

Hi all,

i can generate random number between two numbers in c using this..

arc4random()%(high-low+1)+low;

then now my requirement is...i want to make a number rare....thats mean if

high=5, low=1, and rare=3,

than 3 will be appeared much rarely than 1,2,4 and 5...

Thanks

+9  A: 

One simple-to-understand option:

  • Generate one number to determine whether you're going to return the rare number (e.g. generate a number in the range [0-99], and if it's 0, return the rare number
  • If you get to this step, you're returning a non-rare number: keep generating numbers in the normal range until you get any non-rare number, and return that

There are other alternative approaches which would only require you to generate a single number, but the above feels like it would be the simplest one to write and understand.

Jon Skeet
You don't need to repeatedly generate numbers in the second step, just decrease the range by one and if the random number >= the rare number, increment it.
Skizz
@Skizz: You certainly *could* do that, yes. It would be more efficient. I think it would require more brain cycles than just repeating - at least for me - but it's still a good idea.
Jon Skeet
+17  A: 

You can use tables to calculate your final roll, similar to how pen and paper RPGs do this same type of calculation:

Roll 1 D 21 (easily possibly w/ code).

  • If you get 1-5, it counts as a 1
  • If you get 6-10, it counts as a 2
  • If you get 11-15, it counts as a 4
  • If you get 16-20, it counts as a 5
  • If you get a 21, it counts as a 3

The advantage to this option is you get a strong sense of the exact probabilities you are dealing with. You can get a feeling of exactly how rare or common each number is, and you get fine-grained control of how common each number is, in comparison to the other numbers.

You could also use fractions to generate the table. Use the Least Common Multiple to determine a common base. That base is the max random number size you will need. Then, put all the fractions in like terms. Use the resulting numerators to determine the size of the range for each number in the table.

With this automated solution, the input numbers are very easy to understand in relation to each other. E.g:

  • 1/4 for 1
  • 1/4 for 2
  • 1/4 for 4
  • 1/5 for 5
  • 1/20 for 3

This would generate a table like so:

LCM = 20

  • 1-5 = 1 (like terms - 5/20)
  • 6-10 = 2 (5/20)
  • 11-15 = 4 (5/20)
  • 16-19 = 5 (4/20)
  • 20 = (1/20)

Some more on LCM: http://en.wikipedia.org/wiki/Least_common_multiple

Merlyn Morgan-Graham
+4  A: 

You could create an array containing the numbers according to their probability:

list = (1, 1, 2, 2, 3, 4, 4, 5, 5);
return list.itemAtIndex(random() % list.count());

This is not very elegant, but it works and easily scales should the probabilities get more complex.

zoul
+1; This is a way to implement my solution above, if storage ends up being low, is not a concern, or is a good trade-off vs CPU time
Merlyn Morgan-Graham
A: 
while true
    generate a random number
        if it's not the rare number, return it
    generate a second random number - say from 1 to 100
    if that second number's <= the percentage chance of the rare number compared to the others, return the rare number

Note: this is fast for the common case or returning the non-rare number.

Tony
+1  A: 

The sum of all probabilities must be 1. Now we are working here with discrete probabilities over a finite range so we are looking at (here) 5 possibilities with some distribution you have, call them p1, p2, p3, p4 and p5 the sum of which is 1.

f0 = 0 f1 = p1 f2 = f1 + p2 f3 = f2 + p3 f4 = f3 + p4 f5 = f4 + p5 and must be 1

Generate a random number from 0 to 1 and we will assume it cannot be exactly 1. Look at the f value that fits into its ceiling and that is the value of your random event. So perhaps

f1 = 0.222 f2 = 0.444 f3 = 0.555 f4 = 0.777 f5 = 1

If your random number is 0.645 then you have generated a 4 event. With the above you have half as much chance of generating a 3 than any of the others. We can make it less likely still, eg:

f1 = 0.24 f2 = 0.48 f3 = 0.52 f4 = 0.76 f5 = 1

0.24 probably of the others and only 0.04 of a 3.

CashCow
This solution looks promising, but your explanation is confusing. Can you explain the way you determine which value is selected? The reason I like this answer is that you minimize the calculations involved (random values are always generated between 0.0 and 1.0), and you only require one value be stored per result type.
Merlyn Morgan-Graham
This is elegant, but requires doubles and a binary search through the set of cut-off values. Not the end of the world, but a performance consideration. Doubles are good in another way though - easier to set and understand than the common multiple calculations of Merlyn's approach.
Tony
Yes it would be relatively slow, a faster way is to create a static table. Perhaps we have 256 values in the static table, each of which contains a number from 1 to 5, and you pick a random number from 0 to 255 then look up in the table what value it relates to.
CashCow