phpterryn Posted May 7, 2010 Share Posted May 7, 2010 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: 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 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 createNumbers(43) or 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. Link to comment https://forums.phpfreaks.com/topic/201005-generating-numbers-using-php-and-math/ Share on other sites More sharing options...
trq Posted May 7, 2010 Share Posted May 7, 2010 I hope my explanation for this function is clear enough. I appreciate any help I can get. You need to explain how the numbers in your example output relate to the number (120) in your example input. Link to comment https://forums.phpfreaks.com/topic/201005-generating-numbers-using-php-and-math/#findComment-1054580 Share on other sites More sharing options...
phpterryn Posted May 7, 2010 Author Share Posted May 7, 2010 You need to explain how the numbers in your example output relate to the number (120) in your example input. Thanks for pointing this out. The numbers in my example were just taken out of the blue just to show what I had in mind. I will try to clarify. Exactly which number the function returns is not important, as long as there is some variation to the output. Some thoughts: - The function will return the same set of numbers for each given number. createNumbers(120) will always return the same numbers for each variable, and for createNumbers(121) will always return the same numbers for each variable, but ideally these two numbers will generate different output. - The key is variation in the output. I need a function that can handle any number between 1 and approx 50000, and that generates output that somewhat evenly distributes the numbers. - As an example, if we use the function createNumbers() with input 1 - 100, the variable $num_1_4 (which will return an number between 1 and 4) will return the number 1 approx in 25% of the cases, 2 - 25%, 3 - 25% and 4 - 25% - ideally. Any ideas on how to create a function like this? Link to comment https://forums.phpfreaks.com/topic/201005-generating-numbers-using-php-and-math/#findComment-1054594 Share on other sites More sharing options...
jdavidbakr Posted May 18, 2010 Share Posted May 18, 2010 So you're just basically creating a hash table? Try taking the number and multiplying it by a large prime number and then modding it with your max output. The following will return a predictable array of three numbers between 0-499 given a number input, function createNumbers($num) { $i[1] = ($num * 6133) % 500; $i[2] = ($num * 8011) % 500; $i[3] = ($num * 14653) % 500; } Something like that? You could also use the input as a seed to a random number generator and just return an array of random numbers. Link to comment https://forums.phpfreaks.com/topic/201005-generating-numbers-using-php-and-math/#findComment-1060155 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.