Generate Random number between two number with one

2019-06-15 16:22发布

问题:

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

回答1:

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



回答2:

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.



回答3:

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.



回答4:

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.



回答5:

Lets go through this. First we use the srand() function to seed the randomizer. Basically, the computer can generate random numbers based on the number that is fed to srand(). If you gave the same seed value, then the same random numbers would be generated every time.

Therefore, we have to seed the randomizer with a value that is always changing. We do this by feeding it the value of the current time with the time() function.

Now, when we call rand(), a new random number will be produced every time.

#include<stdio.h>
int random_number(int min_num, int max_num);

int main(void) {
    printf("Min : 1 Max : 30 %d\n",random_number(0,5));
    printf("Min : 100 Max : 1000 %d\n",random_number(100,1000));
    return 0;
}

int random_number(int min_num, int max_num)
{
    int result=0,low_num=0,hi_num=0;
    if(min_num<max_num)
    {
        low_num=min_num;
        hi_num=max_num+1; // this is done to include max_num in output.
    }else{
        low_num=max_num+1;// this is done to include max_num in output.
        hi_num=min_num;
    }

    srand(time(NULL));
    result = (rand()%(hi_num-low_num))+low_num;
    return result;
}


回答6:

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.



标签: c random