Solved

Generate Random Number

Posted on 2004-09-24
6
236 Views
Last Modified: 2008-03-03
Hi All,
I'm using a news script called 'Cutenews' and within this script I can setup different news sections and each of these gets a number 1,2,3,4 etc. To display a news section on my web page I just drop in a piece of php with the appropriate news section number. This works great but I want to change it slightly. I want the news sections to appear randomly
i.e. instead of a variable
$section = 3
I want
$section = a_random_number

I know how to generate a random number between 1-10, 1-20 etc but what I want to know is how to generate a random number from a list of given numbers e.g. if I have the numbers 1,3,4,6,8,9,10,12,15,18, how can I select one of these randomly?

Hope this is clear and many thanks in advance

Michael

0
Comment
Question by:daleoran
  • 3
  • 2
6 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12141965
Hi daleoran,

Have count of number of elements in the list. Generate a number in the range of 1 to count. Pick the counth element for display

Sunnycoder
0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 500 total points
ID: 12142016
Use the rand function

example - random number between 1 and 10

$var = rand(1,10);
echo $var;

To get a random number out of a list you would be best advised to put the numbers in an array and select a random array index.

eg.

$numbers = array(1,3,4,6,8,9,10,12,15,18);
$random = $numbers[rand(0,count($numbers)-1)];
echo $random;
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12142022
you could also just shuffle the array and return the first array item, eg:

$numbers = array(1,3,4,6,8,9,10,12,15,18);
shuffle($numbers);
echo $numbers[0];
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 45

Expert Comment

by:sunnycoder
ID: 12142037
Slight correction


Have count of number of elements in the list. Generate a number in the range of 1 to count.
>Pick the counth element for display

I meant
Pick the randomly generated index number for display
0
 
LVL 10

Author Comment

by:daleoran
ID: 12142546
Many thanks for the prompt replies. Unfortunately I can't try these out until I get home this evening so please be patient

Michael

0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12144897
No problem Michael, if you need any more information later please feel free to post back.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Update database from a form 8 35
Create a Select Query and Populate a Table 3 43
PHP Undefined Index in HTML Form? 2 31
Saving image into server to get url later 1 31
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now