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: 307

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 how to delete plugins in openinviter
2 aababu 233 Wed Sep 01, 2010 7:27 am
aartylamba View latest post
No new posts ERROR IN PAYPAL PRO
0 tapati 20 Tue Aug 31, 2010 12:35 pm
tapati View latest post
No new posts file_exists always return false
0 orenk 114 Mon Aug 16, 2010 9:49 pm
orenk View latest post
No new posts problem with include fuction
0 bluearrow 117 Mon Aug 16, 2010 1:02 pm
bluearrow View latest post
No new posts Javascript not working in smarty .tpl files
1 samir 107 Mon Aug 16, 2010 5:42 am
venkatadapa View latest post
No new posts Add slashes automatically to all post params
0 samir 111 Mon Aug 16, 2010 5:18 am
samir View latest post
No new posts SMTP server settings when send mails through phpmailer class
0 venkatadapa 96 Mon Aug 16, 2010 4:49 am
venkatadapa View latest post
No new posts Parse the xml string content and display in browser using JS
0 venkatadapa 114 Mon Aug 09, 2010 1:22 pm
venkatadapa View latest post
No new posts Parse the xml file content and display in browser using JS
0 venkatadapa 119 Mon Aug 09, 2010 1:18 pm
venkatadapa View latest post
No new posts hipay
1 hassene003 142 Mon Aug 09, 2010 12:35 pm
aababu 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