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

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 music portal script
0 realcoder 0 Thu Mar 11, 2010 8:42 pm
realcoder View latest post
No new posts DataExporter.php anybody make or tell
0 realcoder 3 Thu Mar 11, 2010 8:33 pm
realcoder View latest post
No new posts Email sending through PHP program
1 kumar1116 50 Thu Mar 11, 2010 10:29 am
AnilKumar View latest post
No new posts Php script to display the days between two dates?
1 Thirupathi 55 Thu Mar 11, 2010 9:09 am
Mike View latest post
No new posts display the date in given range
4 Mike 53 Thu Mar 11, 2010 5:31 am
Mike View latest post
No new posts how to add 20 minutes in past date
2 Mike 33 Wed Mar 10, 2010 1:29 pm
Mike View latest post
No new posts Get the random values from mysql
0 AnilKumar 14 Wed Mar 10, 2010 4:43 am
AnilKumar View latest post
No new posts About Expression Engine
1 Thirupathi 72 Mon Mar 08, 2010 2:31 pm
nih View latest post
No new posts content type=text/html and and image/jpeg in same php file?
3 Mike 79 Fri Mar 05, 2010 5:16 am
AnilKumar View latest post
No new posts how to create xls file using mysql and php?
4 AnilKumar 2799 Fri Mar 05, 2010 5:08 am
AnilKumar 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