Jump to content

It Took Hours For Me To Figure This One Function.


beermantm

Recommended Posts

I had to rethink this so many times, I was pulling out the hair in my head. Pulling out the hair that wasn't falling out naturally. If all PHP if this hard to figure out i might go back to css!!

 function randomNums()
	{
		$items = array();
		$results = range(1,10);
		$i = 1;
		
		foreach($results as $result)
		{
			$result = mt_rand(1, 200);
			$items[] = $result;
			
		}
		
		
		$unique = array_unique($items, SORT_NUMERIC);
		$countem = count($unique);
		$unique_counted = $countem -5;
		
		$sum = array_slice($unique, $unique_counted);
		
		
		foreach ($sum as $key)
		{
			$output = $i++.' : '.$key.'<br>';
			echo $output;
		}
		
		
		
		}
	

randomNums();

What do ya think?

Link to comment
Share on other sites

 function randomNums()
	{
		$results_to_gen = 1100;
		$low_range      = 100000;
		$high_range     = 200000;
		$trim_results_to= 1000;
		
		$items = array();
		$results = range( 1, $results_to_gen);
		$i = 1;
		
		foreach($results as $result)
		{
			$result = mt_rand( $low_range, $high_range);
			$items[] = $result;
			
		}
		
		
		$unique = array_unique( $items, SORT_NUMERIC);
		$countem = count( $unique);
		$unique_counted = $countem -$trim_results_to;
		
		$sum = array_slice($unique, $unique_counted);
		
		
		foreach ($sum as $key)
		{
			$output = $i++.' : '.$key.'<br>';
			echo $output;
		}
		
	}
	

randomNums();

CLose to Final result. I think it came out nicely.  Just need to swap out numbers for variables.

Link to comment
Share on other sites

Only Hours? Well done, I have spent days on more than a few, even weeks occasionally.

 

Putting it all together in my mind seems to be the difficult task. Mathematical functions seem easier for my brain to comprehend. I'm definitely learning more and at a faster rate at the moment.  It will all come together soon I feel.

 

Just rinse and repeat, rinse repeat.

 

I could see where deadlines could become a problem.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...