Page 1 of 1

How to create numbers from other number (PHP/MATH)

Posted: Fri May 07, 2010 8:20 am
by terrynowk
This is a challanging task for me, maybe there are smart people here that can solve it easily...

I need a php function that takes a number, and spits out other numbers that are related to this number. Let's call the function for createNumbers();

I will explain this math/php question with an example:

Let's say I have this in php:

Code: Select all

createNumbers(120);
Now the function will take the number 120 (but could be any number from 1 - 50000), and it will return a set of variables.

The stuff returned by createNumbers should be the following variables:

$num_0_1 --> which is a number, either 1 or 0
$num_1_2 --> another number, either 1 or 2
$num_1_3 --> another number, either 1, 2 or 3
$num_1_4 --> another number, either 1, 2, 3 or 4
...
$num_1_30 --> another number, either 1, 2, 3, 4, 5, 6 ... and so on up to 30

So, if I have

Code: Select all

createNumbers(120)
this might return:

$num_0_1 = 1
$num_1_2 = 2
$num_1_3 = 3
$num_1_4 = 1
...
$num_1_30 = 23

And if I have

Code: Select all

createNumbers(43)
or

Code: Select all

createNumbers(8320)
I will get other numbers. But the function must return the same numbers every time for each given number sent as an argument, in other words, I do not want any random functions here.

Don't ask why I need this function, but this is exactly what I need :)
I hope my explanation for this function is clear enough. I appreciate any help I can get.

Re: How to create numbers from other number (PHP/MATH)

Posted: Fri May 07, 2010 8:39 am
by mikosiko
terrynowk wrote:and spits out other numbers that are related to this number
you must define precisely the rules to determinate the relationship

Re: How to create numbers from other number (PHP/MATH)

Posted: Fri May 07, 2010 8:45 am
by terrynowk
Thanks for pointing this out.

It is not important which number the function returns, as long as there is variation. This is the key here I think; variation.

So, for createNumbers(120) it will always return the same value for each variable, but for createNumbers(121) it should preferably return other values.

Any ideas on how to create a function like this?

Re: How to create numbers from other number (PHP/MATH)

Posted: Fri May 07, 2010 9:11 am
by Apollo
Smells like seeding a random generator to me.

Code: Select all

function CreateNumbers( $seed, $num, $min, $max ) // creates $num numbers between $min and $max, depending on $seed
{
  mt_srand($seed);
  $a = array();
  for ($i=0; $i<$num; $i++) $a[] = mt_rand($min,$max);
  mt_srand(); // set mt_rand back to normal (truly random) behaviour
  return $a;
}

Re: How to create numbers from other number (PHP/MATH)

Posted: Fri May 07, 2010 9:53 am
by mikosiko
except for this :
But the function must return the same numbers every time for each given number sent as an argument

Re: How to create numbers from other number (PHP/MATH)

Posted: Fri May 07, 2010 10:35 am
by terrynowk
mikosiko wrote:except for this :
But the function must return the same numbers every time for each given number sent as an argument
Exactly, the function should not return random numbers. However, the values must can be anything within the range for each variable - as long as there is a variation. I.e. if we use the function createNumbers() on every number from 1 to 100, the variable $num_1_4 should return the value 1, 2, 3, and 4 approx 25% of the time each.

I will be very thankful if anyone can help me with this function.

Re: How to create numbers from other number (PHP/MATH)

Posted: Fri May 07, 2010 10:53 am
by social_experiment
terrynowk wrote:Exactly, the function should not return random numbers. However, the values must can be anything within the range for each variable - as long as there is a variation.
Technically speaking you will still have random numbers, just from a known group of numbers. If you have createNumbers(100) for instance :
1.Do you want all values between 0 and 100 or do you want certain values between 0 and 100 and if the second statement is true, how many values do you want?
2.What is the starting point, 0 or 1?

Re: How to create numbers from other number (PHP/MATH)

Posted: Fri May 07, 2010 11:28 am
by Apollo
mikosiko wrote:except for this :
But the function must return the same numbers every time for each given number sent as an argument
That's why I seed the random generator with the given input ($seed).

Same seed = same random sequence being generated.

Re: How to create numbers from other number (PHP/MATH)

Posted: Fri May 07, 2010 1:25 pm
by mikosiko
Apollo wrote:
mikosiko wrote:except for this :
But the function must return the same numbers every time for each given number sent as an argument
That's why I seed the random generator with the given input ($seed).

Same seed = same random sequence being generated.
gottcha... missed that part... my bad :banghead:

Re: How to create numbers from other number (PHP/MATH)

Posted: Sat May 08, 2010 2:49 am
by terrynowk
social_experiment wrote:
terrynowk wrote:Exactly, the function should not return random numbers. However, the values must can be anything within the range for each variable - as long as there is a variation.
Technically speaking you will still have random numbers, just from a known group of numbers. If you have createNumbers(100) for instance :
1.Do you want all values between 0 and 100 or do you want certain values between 0 and 100 and if the second statement is true, how many values do you want?
2.What is the starting point, 0 or 1?
Yes, in a way "random" is what is needed, except it should return the same random numbers each time for the given input.

I.e. createNumbers(120) should return a set of numbers that are "random", but the same numbers should be returned each time. For createNumbers(121) the output should ideally be different from createNumbers(120).

I prefer starting point at 1, except for the variable $num_0_1 which should be set at either 1 or 0. $num_1_4 should be set at either 1, 2, 3, or 4.

This is how the output might look like:

createNumbers(100) => $num_0_1 = 1 ; $num_1_2 = 2 ; ... $num_1_30 = 22
createNumbers(101) => $num_0_1 = 0 ; $num_1_2 = 1 ; ... $num_1_30 = 14
createNumbers(102) => $num_0_1 = 0 ; $num_1_2 = 2 ; ... $num_1_30 = 3
createNumbers(103) => $num_0_1 = 1 ; $num_1_2 = 2 ; ... $num_1_30 = 28

And if I run these three again the variables will be assigned the same "random" values.

Re: How to create numbers from other number (PHP/MATH)

Posted: Sat May 08, 2010 10:35 am
by Apollo
Slightly modified my function, this does exactly what you describe:

Code: Select all

function CreateNumbers( $seed, $num ) // creates $num numbers (where i-th number is a random number <=i), depending on $seed
{
  mt_srand($seed);
  $a = array();
  for ($i=0; $i<$num; $i++) $a[] = mt_rand($i?1:0,$i+1);
  mt_srand();
  return $a;
}

Re: How to create numbers from other number (PHP/MATH)

Posted: Mon May 24, 2010 10:42 am
by terrynowk
thanks a lot, now I have what I need. :D