   PHP Question!!
  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,15I 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?)   ;  hmm.. im still kinda messn with arrays think you could whip up an example?   ;<?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";?>;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  |  Y35 | 2158 | 9059 | 739 | 1253 | 6993 | 3865 | 36100| 8351 | 5637 | 8734 | 5323 | 6929 | 338  | 8945 | 727 | 322 | 9424 | 592 | 115 | 21i need to generate those groups together, so example:If it generates 29, it will generate 33 as well..but split it into 2 variablesso 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 HostileWatersi 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 ):$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);;  but will it keep the 2 groups together at all times?...   ;  k so right now i have it as<?php   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?   