A Simple Php Challenge (2) - Webmasters - Nairaland
Nairaland Forum › Science/Technology › Webmasters › A Simple Php Challenge (2) (954 Views)
| A Simple Php Challenge (2) by elvis10ten(op): 4:11pm On Jul 15, 2013 |
Write a program that will print out a random number between 0 to 100, with numbers from 0 to 50 having about 80% or more chance of occuring again than numbers from 51 to 100. RULES 1)Run your program in www.ideone.com or post it here if you can't. 2)No dirty brute forcing is allowed. 3)The shortest and fastest code wins. 4)Your code should be able to run on a poor pc. Good luck |
| Re: A Simple Php Challenge (2) by Z8(m): 9:26am On Jul 16, 2013 |
/* simulating a 80:100 probability scenerio which is the same as 4:5 */ $probability = mt_rand(1,5); /* give numbers 51-100 a 1:5 probability of occuring */ if($probability == 3){ $random_num = mt_rand(0,50); }else{ $random_num = mt_rand(51,100); } /* $probability is always btw 1-5 ensuring an event happens when its 3 gives it a 1:5 probability */ |
| Re: A Simple Php Challenge (2) by Z8(m): 9:30am On Jul 16, 2013 |
Z8: /* simulating a 80:100 probability scenerioideone.com/6E3p6Q Took 0.01 seconds |
| Re: A Simple Php Challenge (2) by Nobody: 2:18pm On Jul 16, 2013*. Modified: 2:44pm On Jul 16, 2013 |
-- |
| Re: A Simple Php Challenge (2) by elvis10ten(op): 2:34pm On Jul 16, 2013 |
Z8: ideone.com/6E3p6Qyou are close to getting the solution but wrong. Look at your code more and check the output. |
| Re: A Simple Php Challenge (2) by Z8(m): 4:22pm On Jul 16, 2013 |
elvis10ten: you are close to getting the solution but wrong. Look at your code more and check the output./* simulating a 80:100 probability scenerio which is the same as 4:5 */ $probability = mt_rand(1,5); /* give numbers 51-100 a 1:5 probability of occuring */ if($probability == 3){ $random_num = mt_rand(51,100); }else{ $random_num = mt_rand(0,50); } /* $probability is always btw 1-5 ensuring an event happens when its 3 gives it a 1:5 probability */ |
| Re: A Simple Php Challenge (2) by elvis10ten(op): 4:35pm On Jul 16, 2013 |
Z8: /* simulating a 80:100 probability scenerioWhere is your <?php and ?> ?. Where is echo ?. Not because they are common that you need to forget them. |
| Re: A Simple Php Challenge (2) by Z8(m): 5:52pm On Jul 16, 2013 |
elvis10ten: Where is your <?php and ?> ?. Where is echo ?. Not because they are common that you need to forget them.<?php /* simulating a 80:100 probability scenerio which is the same as 4:5 */ $probability = mt_rand(1,5); /* give numbers 51-100 a 1:5 probability of occuring */ if($probability == 3){ $random_num = mt_rand(0,50); }else{ $random_num = mt_rand(51,100); } /* $probability is always btw 1-5 ensuring an event happens when its 3 gives it a 1:5 probability */ echo $random_num; ?> |
| Re: A Simple Php Challenge (2) by elvis10ten(op): 6:45pm On Jul 16, 2013 |
Z8: <?phpcan u get it on www.ideone.com, lets all see the output ?. It would run and its the best and only solution so far. |
| Re: A Simple Php Challenge (2) by Z8(m): 7:14pm On Jul 16, 2013 |
ideone.com/Gbt0Db |
| Re: A Simple Php Challenge (2) by elvis10ten(op): 4:30pm On Jul 22, 2013 |
Bravo, @ Z8 you won man. |
How To Create A Simple PHP Website With PBNL Anybody Can Do It! (naijazoom.com) • What Is Wrong With This Simple PHP - MYSQL Script • Another Simple Php/python/asp/etc Challenge • 2 • 3 • 4
Nairaland Is Currently Worth $6,992,006 in 2014 ! • Understanding The Online Tshirt Business - Teespring • Cool Site Offers Cash To The Very First 4 People Who Want To Admin In The Site