Devin129 Posted October 11, 2013 Share Posted October 11, 2013 I have been trying to figure this out for a while now, so I'm here for help, I am very new to PHP so this is probably pretty simple. I need to generate 5 Sets of numbers non repeating, for a php script im creating. I'm supposed to use the rand() function and each set of numbers is supposed to be in its own array. So how do I create these 5 arrays that will generate random, nonrepeating integers 1-50? I started with this: <?php $random = rand(1,50); $set1=array("$random","$random","$random","$random","$random"); print "$set1[1]"; print "$set1[2]"; print "$set1[3]"; print "$set1[4]"; print "$set1[5]"; ?> - However this obviously will print the same random number, how do I modify it to create the array with 5 unique random numbers in each like below? Auto-Pick 1: 8 12 22 33 45 Auto-Pick 2: 5 15 25 34 48 Auto-Pick 3: 7 12 28 38 42 Auto-Pick 4: 2 13 27 35 49 Auto-Pick 5: 1 18 21 33 50 Quote Link to comment Share on other sites More sharing options...
requinix Posted October 11, 2013 Share Posted October 11, 2013 (edited) Are you specifically supposed to use rand() or can you use something else? For rand() you have to loop for however many you want (like with a for loop) but as you generate numbers only keep ones you haven't used before. That means multiple calls to rand(), not just reusing the same value from one call. While this seems like a good solution, it can be very slow (imagine if you have to generate a set of 10 numbers from 1-10 - that last one could take a while to finish) and you could accidentally hit an infinite loop (if you tried to get 10 numbers from 1-9). A better solution is to generate an array of all the numbers you can choose from, like using range, then a] If you need them in random order, shuffle that array and extract however many you want, or b] If you don't need random order, like your examples show, randomly choose numbers from the array (whose algorithm is much more effective than trying to rand() your way through it). Edited October 11, 2013 by requinix Quote Link to comment Share on other sites More sharing options...
Devin129 Posted October 11, 2013 Author Share Posted October 11, 2013 (edited) Yes, unfortunately my instructions specifically say to use the rand() function. <?php $rand1 = rand(1,50); $rand2 = rand(1,50); $rand3 = rand(1,50); $rand4 = rand(1,50); $rand5 = rand(1,50); $set1=array("$rand1","$rand2","$rand3","$rand4","$rand5"); print "$set1[1]\n"; print "$set1[2]\n"; print "$set1[3]\n"; print "$set1[4]\n"; print "$set1[5]\n"; ?> I now have this so now my issue is the non-repeating part. Is the in_array function what I'm looking for? Edited October 11, 2013 by Devin129 Quote Link to comment Share on other sites More sharing options...
requinix Posted October 11, 2013 Share Posted October 11, 2013 Yes. But you need a loop because you don't know how many times you'll have to try until you get a unique number. Quote Link to comment Share on other sites More sharing options...
Devin129 Posted October 12, 2013 Author Share Posted October 12, 2013 <?php $set1=array(); $set2=array(); $set3=array(); $set4=array(); $set5=array(); for ($set1=0; $set1<5; $set1++) { echo " $set1 \n"; } Okay I have the for loop and it prints 4 numbers, I am still confused on how to randomize and make unique :/ Quote Link to comment Share on other sites More sharing options...
.josh Posted October 12, 2013 Share Posted October 12, 2013 $numbers = array(); while(count($numbers)<25) { array_push($numbers, rand(1,50)); $numbers = array_unique($numbers); } $numbers = array_chunk($numbers, 5); echo "<pre>"; print_r($numbers); Quote Link to comment Share on other sites More sharing options...
.josh Posted October 12, 2013 Share Posted October 12, 2013 more efficient method since it doesn't rely on luck to fill it up $range = range(1,50); shuffle($range); for ($x=0;$x<25;$x++) { $n = array_splice($range,rand(0,count($range)-1),1); $numbers[] = $n[0]; } $numbers = array_chunk($numbers,5); echo "<pre>"; print_r($numbers); I assume your teacher didn't say how to use rand(), only to include it... Quote Link to comment Share on other sites More sharing options...
Devin129 Posted October 12, 2013 Author Share Posted October 12, 2013 (edited) Well the thing is after i Generate these 5 sets, I have to compare them with the winning numbers to see if they are a match. Will this still work if I use your efficient method using range and shuffle? Also how do I get it to only display the numbers and nothing else?? I need it to look like this on the output Auto-Pick 1: 8 12 22 33 45 Auto-Pick 2: 5 15 25 34 48 Auto-Pick 3: 7 12 28 38 42 Auto-Pick 4: 2 13 27 35 49 Auto-Pick 5: 1 18 21 33 50 Edited October 12, 2013 by Devin129 Quote Link to comment Share on other sites More sharing options...
Devin129 Posted October 12, 2013 Author Share Posted October 12, 2013 These are the exact instructions. Please help if possible I am stuck and have been for hours.. You are to create a lottery simulation program. The initial page welcomes the player to the game. Use a very simple form with an explanatory heading then just a field for the player’s name and a button to begin the play. The PHP script should generate five sets of five random numbers (use the rand() function, placing the numbers for each set in a separate array) and display them in the browser. Once the players numbers are generated, a separate set of five “computer generated“ winning numbers are displayed a few lines below and identified as the winning draw for the day. Using for loops and comparing the numbers in each set to the winning set, the script will then check to see if the player has won based on whether their auto picks match 3, 4 or all 5 of the winning numbers. Give them some the option to play as many times as they want by hitting the back button on their browser. It will look something like this when displayed, you can dress it up as you please. By the way, this is not a winning ticket: Auto-Pick 1: 8 12 22 33 45 Auto-Pick 2: 5 15 25 34 48 Auto-Pick 3: 7 12 28 38 42 Auto-Pick 4: 2 13 27 35 49 Auto-Pick 5: 1 18 21 33 50 Today’s Winning Picks: 9 11 29 40 41 All random number sets must follow this rule: 1. Numbers cannot be duplicated within a set. 2. No number can be zero. Winners: Let the user know (by name) how they did (win or lose) by showing them the number of matches to the winning numbers in each set. Prizes are as follows: 1. 3 numbers match = wins $100,000 2. 4 numbers match = wins $250,000 3. 5 numbers match= wins JACKPOT!! $1,000,000 4. Less than 3 numbers matching, no winner. Quote Link to comment Share on other sites More sharing options...
.josh Posted October 12, 2013 Share Posted October 12, 2013 okay seriously, i did the hard part for you - not that even that was hard. All you need is 2 loops, a condition and an echo; php 101. Make an effort man, or else why are you even bothering taking the class? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.