I need to generate random numbers with uneven distribution. eg: I need 25000 random numbers in range 0 - 350 with most values in mid range 100-200 and few values in other ends.
Is there any algorithm , I can use to generate non - uniform distribution.
trying to work on java project. Any language.. c, c++, java, actionscript, python, vb ... is ok.
Copyright © 2024 Q2A.MX - All rights reserved.
Answers & Comments
Verified answer
for algorithm check http://codewordblog.blogspot.com/2011/07/generatio...
Yes you can do it, i do something very similar in the line of work i am in, we call them bias tables, ours don't tend to have 350 entries though.
The way it works is :-
this table has 10 entries (0-9)
num 0
ch 10
num 1
ch 10
num 2
ch 10
num 3
ch 10
num 4
ch 10
num 5
ch 10
num 6
ch 10
num 7
ch 10
num 8
ch 10
num 9
ch 10
Each number has a chance associated with it and all the chances must add up to 100 (or in larger tables 1000 or maybe 10000), in the above example you would get a totally random output as all the numbers have the same chance. But if you reduced the chance on numbers 0 and 9 down to 5 and increased the chance of 4 and 5 up to 15 then you would still get random numbers but more chance of 4 and 5 and less chance of 0 and 9.
This is just off the top of my head so there may be a few minor issues but you get the idea :-
You will have to provide a get_random(n) function that returns a number between 0 and n-1, but you can get that anywhere. Yahoo Answers screws up the formatting but you can just cut n paste.
int p100_func(const int *p100_tab,int pfunc_sz)
{
int tot,n,rnd;
int index=123456; /* setup index with magic number */
tot=0; /* clear total counter */
rnd=get_random(100)+1; /* get random number 1-100 */
for(n=0;n<pfunc_sz;n++) { /* work along the percentage table */
tot+=p100_tab[n]; /* toting up the values */
if(tot>=rnd&&index==123456) /* if total > rnd and index=MAGIC */
index=n; /* set index to current table pos */
}
assert(tot==100);
return(index); /* return the index */
}
This is you bias table (it will have to be a big one for your 350 entries) and in this case adds up to 100
const int bias[]={5,10,10,10,15,15,10,10,10,5};
This function call will return a biased value in the range of 0-9 (in your case it will be 0-349)
p100_func(bias,10);
If you start putting parameters on the result it is not random. Not that you can generate random numbers anyway.