🔒 Locked
PHP Question!!
Advertisement
Original Post
okay I understand how to generate a random number.. but how do you generate a random number using only given numbers? Example: the numbers i want to use are 35,58,59,39,53,93,65,100,51,37,34,23,29,8,45,27,22,24,92,15 I want to output only 1 of those numbers but randomly.. please help, thanks!
The best way to solve your problem is to think of it like this:
1st - You have a total of 20 numbers. If you were to store those numbers into an array, then you would have an "array of numbers" with indices 0 - 19.
2nd - If you were to generate a random number, let's say from 0 - 19, you would be given an index that corresponds to your array of numbers.
3rd - If you were to use that number generated to access your array, you would have effectively choosen a random number from that set of data.
So, if you follow those steps, 1. send all data to an array, 2. generate a random number from 0 to Array Size - 1, 3. Access the array element at that number, you should have your problem solved [smile] Now that you have a basic algorithm, you should be able to find the code. (Although you can just cheat and use a PHP function to get what you want done, but where's the fun in that?)
1st - You have a total of 20 numbers. If you were to store those numbers into an array, then you would have an "array of numbers" with indices 0 - 19.
2nd - If you were to generate a random number, let's say from 0 - 19, you would be given an index that corresponds to your array of numbers.
3rd - If you were to use that number generated to access your array, you would have effectively choosen a random number from that set of data.
So, if you follow those steps, 1. send all data to an array, 2. generate a random number from 0 to Array Size - 1, 3. Access the array element at that number, you should have your problem solved [smile] Now that you have a basic algorithm, you should be able to find the code. (Although you can just cheat and use a PHP function to get what you want done, but where's the fun in that?)
hmm.. im still kinda messn with arrays think you could whip up an example?
Quote:
Original post by Ravuya <?php srand(time()); $numbers = array(1, 46, 342, 666, 1337); $random_index = rand() % sizeof($numbers); $my_random_number = $numbers[$random_index]; echo "Your random number is: ".$my_random_number."\n";?>
wow sweet thanks.. i threw in my numbers and it works.. something i realized though..
i actually need 2 sets of numbers randomized together..
(trying to think how to explain what i need it to do)
think of an XY table..
X | Y
35 | 21
58 | 90
59 | 7
39 | 12
53 | 69
93 | 38
65 | 36
100| 83
51 | 56
37 | 87
34 | 53
23 | 69
29 | 33
8 | 89
45 | 7
27 | 3
22 | 94
24 | 5
92 | 1
15 | 21
i need to generate those groups together, so example:
If it generates 29, it will generate 33 as well..but split it into 2 variables
so like $x_GEN = 29 and $y_GEN = 33... or 15 and 21.. see what im saying?
what im doing is making it for my game, when a player registers it generates a number and it will save the X in one table on the database and the Y in another..
lemme know if you need a little more explanation...
thanks!
Quote:
Original post by HostileWaters
i actually need 2 sets of numbers randomized together..
(trying to think how to explain what i need it to do)
Ravuya's example would work for that as well:
<?php srand(time()); $x = array(35, 58, 59 , 39 ); $y = array(21, 90, 7, 12); $random_index = rand() % sizeof($numbers); $the_x = $x[$random_index]; $the_y = $y[$random_index];?>Once you know your index, "random_index", then you are all set, you just access that index of all the data arrays.
Use a multi-dimentional array (or an array of arrays ):
Then access the elements like was stated by Ravuya:
Then access the individual elements of that array like this:
Hope that helps. That actually spelled it all out...
$numbers = array( array(35, 21), array(58, 90), array(59, 7), ... )Then access the elements like was stated by Ravuya:
$my_random_numbers = $numbers[$random_index];Then access the individual elements of that array like this:
$x = $my_random_numbers[0];&y = &my_random_numbers[1];Hope that helps. That actually spelled it all out...
Umm.... why....?
$numbers = array(35,58,59,39,53,93,65,100,51,37,34,23,29,8,45,27,22,24,92,15);
$number = $numbers[array_rand($numbers)];
or...
$numbers = array(35,58,59,39,53,93,65,100,51,37,34,23,29,8,45,27,22,24,92,15);
shuffle($numbers);
$number = reset($numbers); // or $number = end($numbers);
$numbers = array(35,58,59,39,53,93,65,100,51,37,34,23,29,8,45,27,22,24,92,15);
$number = $numbers[array_rand($numbers)];
or...
$numbers = array(35,58,59,39,53,93,65,100,51,37,34,23,29,8,45,27,22,24,92,15);
shuffle($numbers);
$number = reset($numbers); // or $number = end($numbers);
but will it keep the 2 groups together at all times?...
k so right now i have it as
srand(time());
$X_Loc = array(35, 58, 59, 39, 53, 93, 65, 100, 51, 37, 34, 23, 29, 8, 45, 27, 22, 24, 92, 15);
$Y_Loc = array(21, 90, 7, 12, 69, 38, 36, 83, 56, 87, 53, 69, 33, 89, 7, 3, 94, 5, 1, 21);
$random_index = rand() % sizeof($X_Loc);
$random_index2 - rand() % sizeof($Y_Loc);
$the_x = $x[$random_index];
$the_y = $y[$random_index2];
echo "The X is $the_x";
echo "The Y is $the_y";
?>
but this isnt doing anything it wont produce the 2 numbers, ideas?
srand(time());
$X_Loc = array(35, 58, 59, 39, 53, 93, 65, 100, 51, 37, 34, 23, 29, 8, 45, 27, 22, 24, 92, 15);
$Y_Loc = array(21, 90, 7, 12, 69, 38, 36, 83, 56, 87, 53, 69, 33, 89, 7, 3, 94, 5, 1, 21);
$random_index = rand() % sizeof($X_Loc);
$random_index2 - rand() % sizeof($Y_Loc);
$the_x = $x[$random_index];
$the_y = $y[$random_index2];
echo "The X is $the_x";
echo "The Y is $the_y";
?>
but this isnt doing anything it wont produce the 2 numbers, ideas?
I'd also like to point out that PHP has a function called array_rand. It basicaly picks random entries out of an array. You can find more information and examples here.
For a quick example using the above:
srand(time());
$numbers = array(1, 46, 342, 666, 1337);
$random_index = array_rand($numbers);
$my_random_number = $numbers[$random_index];
echo "Your random number is: ".$my_random_number."\n";
?>
For a quick example using the above:
srand(time());
$numbers = array(1, 46, 342, 666, 1337);
$random_index = array_rand($numbers);
$my_random_number = $numbers[$random_index];
echo "Your random number is: ".$my_random_number."\n";
?>
Quote:
Original post by Ithir
I'd also like to point out that PHP has a function called array_rand. It basicaly picks random entries out of an array. You can find more information and examples here.
For a quick example using the above:
srand(time());
$numbers = array(1, 46, 342, 666, 1337);
$random_index = array_rand($numbers);
$my_random_number = $numbers[$random_index];
echo "Your random number is: ".$my_random_number."\n";
?>
yes i am aware of this, but what im saying is I need it to random in groups... so if x is 29, y HAS to be 33 or if x is 65, y HAS to be 36... see what im saying?
I may have to just random the first one, and then put an if then thing.. like if x=29 then y=33... but that just makes the application slower ya know?
Quote:
srand(time());
$X_Loc = array(35, 58, 59, 39, 53, 93, 65, 100, 51, 37, 34, 23, 29, 8, 45, 27, 22, 24, 92, 15);
$Y_Loc = array(21, 90, 7, 12, 69, 38, 36, 83, 56, 87, 53, 69, 33, 89, 7, 3, 94, 5, 1, 21);
$random_index = rand() % sizeof($X_Loc);
$random_index2 - rand() % sizeof($Y_Loc);
$the_x = $x[$random_index];
$the_y = $y[$random_index2];
echo "The X is $the_x";
echo "The Y is $the_y";
?>
You're calling rand() twice in there, which selects two random indices (one for each array) ... messing up the whole point of arranging the numbers in the first place! Try actually doing what Drew said earlier.
Quote:
Original post by Bob Janova Quote:
srand(time());
$X_Loc = array(35, 58, 59, 39, 53, 93, 65, 100, 51, 37, 34, 23, 29, 8, 45, 27, 22, 24, 92, 15);
$Y_Loc = array(21, 90, 7, 12, 69, 38, 36, 83, 56, 87, 53, 69, 33, 89, 7, 3, 94, 5, 1, 21);
$random_index = rand() % sizeof($X_Loc);
$random_index2 - rand() % sizeof($Y_Loc);
$the_x = $x[$random_index];
$the_y = $y[$random_index2];
echo "The X is $the_x";
echo "The Y is $the_y";
?>
You're calling rand() twice in there, which selects two random indices (one for each array) ... messing up the whole point of arranging the numbers in the first place! Try actually doing what Drew said earlier.
okay so I used
srand(time());
$x = array(35, 58, 59 , 39 );
$y = array(21, 90, 7, 12);
$random_index = rand() % sizeof($numbers);
$the_x = $x[$random_index];
$the_y = $y[$random_index];
echo "X is $the_x";
echo "Y is $the_y";
?>
it wont randomize them though, it only displays 35,21... any ideas?
[Edited by - HostileWaters on May 27, 2006 6:55:13 PM]
ah hah.. there we go! changed $numbers to $x
thanks!
thanks!
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement