Status
Not open for further replies.
Maybe you should explain again what you want it to do. I thought you wanted to generate a random numbers that with each generated number have a higher probability of generating a bigger value. Since your friend suggested a matrix with a curve etc.
Sorry I probably should have explained this more at the start. It's to pick topics randomly from a database but I want it to concentrate on picking the newest topics. I'm ordering the topics by latest posts and want it to pick one at random and run a script. I don't want it running the script too often on topics that are a year or two old.

It would be useful for eg. a link checker but I've other ideas I want to use it on first eg. a fake account which replies to recent topics and only rarely replies and bumps an old topic.

That's why I want it to pick lower numbers more often and as the number grows the chance of it being picked decreases.

As I said your earlier example is fine for the purpose of the script and this topic is just improving the selection method and my knowledge which isn't exactly necessary but is helpful and greatly appreciated.
 
And do you want to generate multiple random numbers at roughly the same time in the script or do you just need 1 per run?

One per run would be fine but ideally multiples of a small amount. say something like 10 topics at a time. I wouldn't mind having to run it 10 times though in the loop.
 
This is actually quite a complex problem which causes my brain to get stuck in an infinite loop.

rand(min, max) - if I understand it right the max value should be different every time you need a random number. And the max value(s) should give a smaller random number a higher probability than a large one. But at the same time you cant use something like x procent because you want each step in the max value to have its own probability (damn this is hard to explain :(). For example you want a random number between 1 and 1000. You want a higher probability for lower values. but there are 1000 'steps' in the 1-1000 range and you want each of those steps to have its own probability?

If so: that's is a problem. Or at least it is for me because this is a chicken-egg situation. The probability should be random too per step. But how do you randomize the probability of the probability (lol).

*brain fried*

These are times I wish I had paid more attention during math lessons in school. A matrix will do little good here because that's linear.
 
this is certainly a very complex problem.

I think a simple solution would be to create an array with multiple numbers. Something like below

$x[] = 1;
$x[] = 2;
$x[] = 2;
$x[] = 2;

Simple example so basically you can increase the chances of certain numbers that appear.

so if you want 2 to appear more then 1 then 3 out of 4 times. This is a small scale example. This would be using rand(0, 4-1)
 
This is actually quite a complex problem which causes my brain to get stuck in an infinite loop.
lol that's good.

rand(min, max) - if I understand it right the max value should be different every time you need a random number. And the max value(s) should give a smaller random number a higher probability than a large one. But at the same time you cant use something like x procent because you want each step in the max value to have its own probability (damn this is hard to explain :(). For example you want a random number between 1 and 1000. You want a higher probability for lower values. but there are 1000 'steps' in the 1-1000 range and you want each of those steps to have its own probability?

If so: that is a problem. Or at least it is for me because this is a chicken-egg situation. The probability should be random too per step. But how do you randomize the probability of the probability (lol).

*brain fried*

These are times I wish I had paid more attention during math lessons in school. A matrix will do little good here because that's linear.

Well what I'm doing is one forum at a time to make it easier so the max will be constant at lease for the loop it's doing. So the max will be something like count() FROM thread WHERE forumid=$whatever if that helps (although I don't think it does much).
but ya everything else you have correct theory wise. That's why I asked in the original post if someone even knows the mathematical term for this so I could maybe research it. My problem is I haven't studied advanced Maths in a few years now so don't even know what you'd call this.


@ immortalxx: I don't see how that is scalable. Remember a forum can contain 100k plus topics.
 
Here you go Happy, Ive created a function that you insert any number and it will try bring you back a random with the chances leaning towards the lower integer.

so heres the functions

PHP:
function Random($highest = 100,$strength = 2)
{
   $r = round($highest/$strength);
   return abs((rand()%(($r*2)+($r)))-$r);
}

Ok so here an example.

PHP:
$high = $low = 0;

for($i=0;$i<5000;$i++)
{
    if(Random(6478,1) > (6478/2)) //Check see if its higher or lower then the count
    {
        $high++;
    }else
    {
       $low++;
    }
}

echo $low . '/' . $high;

This is a test to see how the chances are over 500 loops on 6478 rows/

some results are

Using strength 2:
  • Low(3385) / High(1615)
  • Low(3271) / High(1729)
  • Low(3326) / High(1674)
Using strength 1:
  • Low(2524) / High(2476)
  • Low(2537) / High(2463)
  • Low(2473) / High(2527)

For strength only use 1,2 or 3.

So yes, usage just call the functions with the first param as the MAX Integer and the second as strength, usually 2 tho..
 
To be honest I've just used a version Hyperz originally posted. For the script it's not essential it's such a randomized pick as it works fine. I was just interested to know if it was possible as it would improve it slightly. Sense it's such a complicated request with such a minor improvement I've no problem admitting defeat.

Again massive thanks for all your help and replies guys. It's such a pleasure to be able to get such fast help in this in coding area rather than having to go to a dedicated coding site.
 
Status
Not open for further replies.
Back
Top