Senior & Expert PHP Developers Discussion Forum by Nyros Technologies

HIRE PHP Expert Developers Programmers Coders From India
PHP .Net Ruby on Rails Developers Community, Nyros Technologies, Kakinada
 
Log in  or IF not a member please REGISTER
Username:
Password:   


Keyword
Log in | Profile 

Problem with random combinations because of unique need

 
Post new topic   Reply to topic    Senior & Expert PHP Developers Discussion Forum by Nyros Technologies Index -> Help
View previous topic :: View next topic  
Author Message
artech



Joined: 30 Jun 2009
Posts: 1

PostPosted: Tue Jun 30, 2009 1:43 pm    Post subject: Problem with random combinations because of unique need Reply with quote

Hi all,

I'm looking for some direction in how to approach this problem.

$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);

I need to create a second array using the values in the first array in a random order, without repeating or omitting any of the values, except one of them.

I'm having such a hard time with this next part, and I even struggle to explain it. So please clarify as needed.

I need to fill the second array with a random combination of the first array, with one value repeated. But the value that is repeated has to be randomly selected from the first five positions of the new array, and it has to be placed randomly into one of the last five positions of the new array.

so assuming that the first five values of the randomly generated array are:
3, 7, 8, 19, 14

I would need to continue randomly filling the 2nd array with values from the first array ($arr, above) until I fill position 15 (the 16th value). Then positions 16-20 (17th through 21st values) need to have the 4 remaining numbers and one of the numbers from the first five spots (3, 7, 8, 19, or 14 in this example).

So that the final array would look something like this

$arr2 = array(
0 => 3,
1 => 7,
2 => 8,
3 => 19,
*4 => 14,

5 => 1, 6=>2, 7=>4, 8=>20, 9=>18, 10=>16, 11=>15, 12=>12, 13=>11, 14=>6, 15=>10,

16 => 17,
17 => 5,
*18 => 14,
19 => 9,
20 => 13);

I've indicated the repeated number with an asterisk and have used position => value nomenclature hoping for clarity.

In this case the value in position 4 is repeated in position 18.

I need this part randomly figured out during each iteration in addition to randomly filling all the original values contained in the first array. So the example above would be only one possible outcome.

Thanks in advance for any guidance you can provide. Please let me know if what I'm asking is not clear and I will try to clarify as needed.
Back to top
View user's profile Send private message
venkatadapa



Joined: 12 May 2008
Posts: 250

PostPosted: Wed Jul 01, 2009 6:04 am    Post subject: Reply with quote

Hi artech,
Below code might be help you. It will create second array from first array values and last element will be any from first five.

<?php
$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
$numbers = range($arr[0],$arr[19]);
$arr2=array();
shuffle($numbers);
for($i=0;$i<20;$i++)
{
array_push($arr2,$numbers[$i]);
}
$rand=rand('0','4');
array_push($arr2,$arr2[$rand]);
//Now $arr2 contains the output
print_r($arr2);
?>

Bye Laughing Laughing Laughing
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Senior & Expert PHP Developers Discussion Forum by Nyros Technologies Index -> Help
Page 1 of 1

 latest topics 
 Topics   Replies   Author   Views   Last Post 
No new posts highlet source code in php application
0 Sanjana 5 Thu Mar 18, 2010 11:04 am
Sanjana View latest post
No new posts Get the status from twitter using PHP script
1 Thirupathi 29 Tue Mar 16, 2010 4:41 am
Phani Kumar View latest post
No new posts Error: CakephpController could not be found.
0 Mike 17 Tue Mar 16, 2010 4:21 am
Mike View latest post
No new posts cake php
2 Mike 35 Mon Mar 15, 2010 11:46 am
Mike View latest post
No new posts Problem to create a folder in my server
1 samir 19 Mon Mar 15, 2010 7:32 am
rajkumar View latest post
No new posts query in many table..how to read it
0 mun 20 Sun Mar 14, 2010 8:03 am
mun View latest post
No new posts search data in many table
0 mun 16 Sun Mar 14, 2010 7:57 am
mun View latest post
No new posts HipHop for PHP is now opensource
0 venkatadapa 23 Sat Mar 13, 2010 7:08 am
venkatadapa View latest post
No new posts music portal script
0 realcoder 16 Thu Mar 11, 2010 8:42 pm
realcoder View latest post
No new posts DataExporter.php anybody make or tell
0 realcoder 17 Thu Mar 11, 2010 8:33 pm
realcoder View latest post





Hire an expert PHP developer / coder / programmer or development team from India now!!

Other Forums : Ruby on Rails   ::   .Net   |   Free unlimited HTML CSS Joomla Wordpress Drupal templates download

Nyros Technologies   |   Developers Blog   |   Kakinada City Portal   |   About PHP Experts   |   More