[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to use the mt_rand function, so it wont repeat?

Posted on 2008-11-12
4
Medium Priority
?
610 Views
Last Modified: 2012-05-05
Hi all

I am using a mt_rand function to set different styles each time a user clicks on my page, how can i make sure it will not repeat the same style after a refresh?

<?php
#############################################################################
function SetStyle(){
 
    $style[] = "css/n1.css";
	$style[] = "css/n2.css";
	$style[] = "css/n3.css";
	$style[] = "css/n4.css";
	$style[] = "css/n5.css";
	$style[] = "css/n6.css";
	$number = mt_rand(0,5);
	$stylesheet = $style[$number];
 
 return $stylesheet;
}
?>

Open in new window

0
Comment
Question by:PeterdeB
4 Comments
 
LVL 27

Accepted Solution

by:
Cornelia Yoder earned 2000 total points
ID: 22941610
Unless you save the previous values and check against them, you cannot guarantee this.  Random is random, and occasionally a die rolls the same number twice in a row.
0
 
LVL 6

Expert Comment

by:Werrock
ID: 22941718
You could save the old value in a session and always run the rand function while it returns the same value as saved in the session.
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22942572
Possibly not the best application of a random function (seems like you could just cycle through them one by one), with the random function as applied on average you expect 1 in 6 numbers to be a duplicate of the previous number, consider this code to illustrate:
<pre>
<?php
$number=0;
$oldnumber=0;
$dup=0;
for($i = 0; $i < 1000000; $i++) {
	$number = mt_rand(0,5);
	if($oldnumber==$number){
		$dup++;
	}
	$oldnumber=$number;
}
echo "There were $dup duplicates out of 1,000,000.";
?>
 
Output:
There were 166103 duplicates out of 1,000,000.

Open in new window

0
 

Author Comment

by:PeterdeB
ID: 22947704
thank you, but will using a different function fix this? Well i will post a new question, since this question is answered by the first expert, again thanks all
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

872 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