Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-30-2005, 10:26 AM   #1 (permalink)
coryw
Registered User
 
coryw's Avatar
 
Join Date: Aug 2005
Posts: 5
coryw is on a distinguished road
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.
coryw is offline   Reply With Quote
Old 08-30-2005, 10:35 AM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
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
teknomage1 is offline   Reply With Quote
Old 08-30-2005, 10:59 AM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,694
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 08-30-2005, 11:05 AM   #4 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 596
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
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
teknomage1 is offline   Reply With Quote
Old 08-30-2005, 11:50 AM   #5 (permalink)
coryw
Registered User
 
coryw's Avatar
 
Join Date: Aug 2005
Posts: 5
coryw is on a distinguished road
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...
coryw is offline   Reply With Quote
Old 08-30-2005, 12:19 PM   #6 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 651
DJMaze is on a distinguished road
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']%!= 0) {
    if (
$_SESSION['one']*$_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
DJMaze is offline   Reply With Quote
Old 08-30-2005, 12:38 PM   #7 (permalink)
coryw
Registered User
 
coryw's Avatar
 
Join Date: Aug 2005
Posts: 5
coryw is on a distinguished road
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...
coryw is offline   Reply With Quote
Old 08-30-2005, 01:04 PM   #8 (permalink)
coryw
Registered User
 
coryw's Avatar
 
Join Date: Aug 2005
Posts: 5
coryw is on a distinguished road
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.
coryw is offline   Reply With Quote
Old 08-30-2005, 01:25 PM   #9 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,446
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 08-30-2005, 06:31 PM   #10 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 651
DJMaze is on a distinguished road
sorry i went a bit to sophisticated coryw

I hope you learn a lot
DJMaze is offline   Reply With Quote
Old 08-30-2005, 09:46 PM   #11 (permalink)
coryw
Registered User
 
coryw's Avatar
 
Join Date: Aug 2005
Posts: 5
coryw is on a distinguished road
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.
coryw is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 04:46 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting