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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
terrynowk
Forum Newbie
Posts: 6
Joined: Thu Jun 11, 2009 4:10 am

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

Post 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.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

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

Post 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
terrynowk
Forum Newbie
Posts: 6
Joined: Thu Jun 11, 2009 4:10 am

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

Post 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?
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

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

Post 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;
}
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

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

Post by mikosiko »

except for this :
But the function must return the same numbers every time for each given number sent as an argument
terrynowk
Forum Newbie
Posts: 6
Joined: Thu Jun 11, 2009 4:10 am

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

Post 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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

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

Post 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?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

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

Post 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.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

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

Post 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:
terrynowk
Forum Newbie
Posts: 6
Joined: Thu Jun 11, 2009 4:10 am

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

Post 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.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

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

Post 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;
}
terrynowk
Forum Newbie
Posts: 6
Joined: Thu Jun 11, 2009 4:10 am

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

Post by terrynowk »

thanks a lot, now I have what I need. :D
Post Reply