|
 |
|
 |
08-30-2005, 10:26 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Aug 2005
Posts: 5
|
help with rand function...
Hi there,
Disclaimer: This is my 3rd day learning PHP, so the following is likely going to sound silly:
Problem: The book I am using to teach myself (PHP5/MySQL for the absolute beginner by Andy Harris) has "challenges" at the end of each chapter, and I am stuck on a simple one...
"Write a 'loaded dice' program that half the time generates the value 1 and half the time generates some other value"
The basic die rolling looks like this:
<?
$roll = rand(1,6);
print "You rolled a $roll";
?>
When the html page containing the code is refreshed, it regenerates with a new random roll number.
My first idea was to make if statements for half of the possible numbers rolled (1-3 or 4-6, for a 6 sided die etc.) and then just have PHP print "You rolled a one" - but that won't really be "half of the time" because, well, it's random...
Am I missing some really simple here? Or is there another function I could add in to help in delivering the "1" value 50% of the time?
TIA for any help.
|
|
|
08-30-2005, 10:35 AM
|
#2 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
|
why not write something like this
Code:
<?php
if( rand(0,1) ) {
$roll = rand(2,6);
print "You rolled a $roll";
}
else {
print "You rolled a 1";
}
?>
That way you're mathematically garunteed to roll 1 fifty percent of the time, thanks to the first if statement. But taking 1-3 out of a six sided roll is also 50%.
__________________
Stop intellectual property from infringing on me
|
|
|
08-30-2005, 10:59 AM
|
#3 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,705
|
Quote:
|
But taking 1-3 out of a six sided roll is also 50%.
|
Then it wouldn't be an evenly displaying of the numbers 2 - 6 appart from the fact, that 1 is reprecented 50% of the rolls.
|
|
|
08-30-2005, 11:05 AM
|
#4 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
|
Quote:
|
Originally Posted by redhead
Then it wouldn't be an evenly displaying of the numbers 2 - 6 appart from the fact, that 1 is reprecented 50% of the rolls.
|
True but the objective is just to display 1 fifty percent of the time and something else the other half of the time.
__________________
Stop intellectual property from infringing on me
|
|
|
08-30-2005, 11:50 AM
|
#5 (permalink)
|
|
Registered User
Join Date: Aug 2005
Posts: 5
|
Thanks for the input!
I'm not exactly sure how to test this...
In theory, when we land on the page, it should show us a one, and when we refresh, it should show another number (between 2 and 6), or, the opposite (first a number between 2-6, followed by a 1).
So, in a successive number of even rolls I would expect to see half of the rolls delivering a 1 (for example over 12 rolls, I should see 6 ones). But its not quite doing that.
The page and code is up at (ignore the banners, it's a free host...)
http://corwhi.tollfreepage.com/test_...loadedDice.php
Am I missing something? Sorry to be obtuse here...
|
|
|
08-30-2005, 12:19 PM
|
#6 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 672
|
rand(0,1) isn't accurate enough.
This is because 50% of the time:
2 rolls = 1x1
4 rolls = 2x1
6 rolls = 3x1
There's no guarantee that if you use rand(0,1) 2 times it returns 1x0 and 1x1
To avoid this you should use a cookie or session.
Inside the cookie/session you count how much times the dice is rolled and how much times it was '1'
like:
PHP Code:
<?php
session_start();
if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
$_SESSION['one'] = 0;
}
if ($_SESSION['count']%2 != 0) {
if ($_SESSION['one']*2 < $_SESSION['count']) {
$val = 1;
} else {
$val = rand(2,6);
}
} else {
$val = rand(1,6);
}
++$_SESSION['count'];
if ($val == 1) ++$_SESSION['one'];
echo $val;
This is the most accurate but it does things like: 15411614411513
|
|
|
08-30-2005, 12:38 PM
|
#7 (permalink)
|
|
Registered User
Join Date: Aug 2005
Posts: 5
|
Thanks DJMaze,
As I am just getting started with PHP (I only know HTML at this point), this is a bit over my head, but I'll hang onto this and try to use it once I have a better understanding of cookie(s)/session(s).
Since the "problem/assignment" is from the beginning (chapter 3: conditions and fucntions) of a "beginners" book, I wonder if there is a more simple solution I am just missing somehow...
|
|
|
08-30-2005, 01:04 PM
|
#8 (permalink)
|
|
Registered User
Join Date: Aug 2005
Posts: 5
|
Um... I just realized I was equating alternating "1" and "other values" with 1 showing up 50% of the time - which is not the same thing... I was making this more complicated than it needed to be!
I suppose if I really want to test if 1 and other values (between 2-6) are showing up 50 % of the time, I would want to write something that kept running the page over and over, and tracked the results... But that's more than the assignment asked.
D'oh!
Thanks all.
|
|
|
08-30-2005, 01:25 PM
|
#9 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,486
|
yeah i think although djmaze has a good point about the question in that you would have to maintain state in order to guarantee 1 showing up 50% of the time, .. but being that it's a beginner's book, i think that technomage has the right idea.
i would inturprit this problem as "make loaded six sided dice that has a 50% chance of rolling a 1.
and welcome to the site 
__________________
Mike
|
|
|
08-30-2005, 06:31 PM
|
#10 (permalink)
|
|
Senior Contributor
Join Date: Mar 2005
Posts: 672
|
sorry i went a bit to sophisticated coryw
I hope you learn a lot 
|
|
|
08-30-2005, 09:46 PM
|
#11 (permalink)
|
|
Registered User
Join Date: Aug 2005
Posts: 5
|
No problem. It's fun stuff! Hopefully, in a while this book I'm using will seem like child's play.
Thanks to everyone for the input. 
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 04:38 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|