Jump to content

Recommended Posts

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
 
Link to comment
https://forums.phpfreaks.com/topic/282901-generating-random-non-repeating-array/
Share on other sites

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 by requinix

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 by Devin129
   <?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 :/

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...

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 by Devin129
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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.