Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Displaying a random string

string

  • Please log in to reply
1 reply to this topic

#1 RHochstenbach

RHochstenbach

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 56 posts

Posted 14 March 2012 - 02:23 PM

The following tutorial is useful to display random strings. Why would you want to do such a thing? One of the uses is displaying a Message of the Day. In this example I will display a random letter from the alphabet.

1. Define an array.
$my_array = array();


2. Create a key for each string. You don't have to specify an index number (the number inside the brackets [ ] ) for each key, but I am doing so to make the flow better understandable.

$my_array[0] = "A";
$my_array[1] = "B";
$my_array[2] = "C";
$my_array[3] = "D";
$my_array[4] = "E";
$my_array[5] = "F";
$my_array[6] = "G";
$my_array[7] = "H";
$my_array[8] = "I";
$my_array[9] = "J";
$my_array[10] = "K";
$my_array[11] = "L";
$my_array[12] = "M";
$my_array[13] = "N";
$my_array[14] = "O";
$my_array[15] = "P";
$my_array[16] = "Q";
$my_array[17] = "R";
$my_array[18] = "S";
$my_array[19] = "T";
$my_array[20] = "U";
$my_array[21] = "V";
$my_array[22] = "W";
$my_array[23] = "X";
$my_array[24] = "Y";
$my_array[25] = "Z";


3. We need to store the highest index number (in this case '25') in a variable. This array has 26 items, but the highest index number is '25' because arrays start at '0'. So we count the size of the array using count($my_array), and then subtract '1' from it.
$array_size = count($my_array) - 1;


4. Now generate a random number between '0' (the lowest number in the array) and the value you've stored in the variable above. This will generate a random index number that matches an index number inside your array.
$random_index = mt_rand(0,$array_size);


5. Print the random key by putting the generated number above inside the brackets.

echo $my_array[$random_index];


The complete code:

// An array with some text.
// Modify it to contain the random texts it will show.
$my_array = array();
$my_array[0] = "A";
$my_array[1] = "B";
$my_array[2] = "C";
$my_array[3] = "D";
$my_array[4] = "E";
$my_array[5] = "F";
$my_array[6] = "G";
$my_array[7] = "H";
$my_array[8] = "I";
$my_array[9] = "J";
$my_array[10] = "K";
$my_array[11] = "L";
$my_array[12] = "M";
$my_array[13] = "N";
$my_array[14] = "O";
$my_array[15] = "P";
$my_array[16] = "Q";
$my_array[17] = "R";
$my_array[18] = "S";
$my_array[19] = "T";
$my_array[20] = "U";
$my_array[21] = "V";
$my_array[22] = "W";
$my_array[23] = "X";
$my_array[24] = "Y";
$my_array[25] = "Z";

// Get the size of the array for getting the highest index number.
// Arrays start at '0', so we subtract 1 from the total amount.
// Key number '26' will have an index of '25'
$array_size = count($my_array) - 1;

// Generate a number between 0 and the highest index number of the array
$random_index = mt_rand(0,$array_size);

// Print the array key with the generated index number
echo $my_array[$random_index];

  • 0

#2 surajkumardotin

surajkumardotin

    CC Regular

  • Member
  • PipPipPip
  • 29 posts
  • Location:Ludhiana
  • Programming Language:PHP, JavaScript
  • Learning:C, C++, PHP, JavaScript

Posted 18 August 2015 - 11:58 PM

Why are not using array rand() function.

<?php
echo rand('10','100');
?>

Edited by surajkumardotin, 18 August 2015 - 11:59 PM.

  • 0