/    Sign up×
Community /Pin to ProfileBookmark

conditionals Dice roll, average

Hi, I am having trouble creating a page that stimulates a large number of dice rolls and computes the average of the dice totals in an separate txt box.

if anyone can please help me,I would be appreciated
thank you

-chelle

[upl-file uuid=03081de1-85b4-4dec-b43f-524a701fdccb size=2kB]avg1.txt[/upl-file]

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@javaNoobieNov 04.2004 — <i>
</i>&lt;script type="text/JavaScript"&gt;
&lt;!--
function roll(f){
var numOfRolls = 5; //indicate number of roll
var total = 0;
for(var i=0;i&lt;numOfRolls;i++){
total += Math.ceil(Math.random() * 6);
}
var avg = (total/numOfRolls).toString();
f.average.value = avg;
}
//--&gt;
&lt;/script&gt;


&lt;form name="form1" action=""&gt;
&lt;span&gt;Average: &lt;input type="text" name="average" readonly&gt;&lt;/span&gt;
&lt;button onclick="roll(this.form)"&gt;Lets Roll!&lt;/button&gt;
&lt;/form&gt;
Copy linkTweet thisAlerts:
@chelle22authorNov 04.2004 — alright thanks! I'll try that
Copy linkTweet thisAlerts:
@chelle22authorNov 05.2004 — I added more script to it, but now i keep getting errors..can someone please check this for me?

thanks


----------------------------------------------------------
<script type="text/JavaScript">

<!--


function roll(f)

{

var numOfRolls, repCount, roll1, roll2;

var numOfRolls = 6; //indicate number of roll
var total = 0;
for(var i=0;i<numOfRolls;i++){
total += Math.ceil(Math.random() * 6);
}
var avg = (total/numOfRolls).toString();
f.average.value = avg;



repCount = 0;
while (repCount <numOfRolls) { // repeatedly:
roll1 = RandomInt(1, 6); // simulate the dice rolls
roll2 = RandomInt(1, 6);

}

repCount = repCount + 1; // incr. repetition counter

}

//-->

</script>


<form name="form1" action="">

<span>Average: <input type="text" name="average"></span>

<button onclick="roll(this.form)">Click to Roll</button>

Desired number of rolls =

<input type="text" name="reps" size=6 value=1000 />

<br /><br />

</form>

[upl-file uuid=f6d2a8ac-5378-442d-b5d7-3f8cfd4b404e size=906B]avgr.txt[/upl-file]
Copy linkTweet thisAlerts:
@javaNoobieNov 05.2004 — What are u trying to incoporate? Where is RandomInt() function defined?? And please post what kind of errors you are getting.
Copy linkTweet thisAlerts:
@senshiNov 05.2004 — [CODE]<script type="text/JavaScript">
<!--
function rollDie(){return Math.ceil(Math.random() * 6);}

function roll(numOfRolls){
var repCount, roll1, roll2, totals = 0;
for(i=0; i < numOfRolls; i++){
totals+=rollDie();
} // end of for
var avg = totals / numOfRolls;
document.form1.average.value=avg;
} // end of function
//-->
</script>[/CODE]


and change
[CODE]
<button onclick="roll(this.form)">Click to Roll</button>
[/CODE]

to
[CODE]
<input type="button" name="button" value="Roll Me" onclick="roll(document.form1.reps.value);">
[/CODE]


And it appears to work, if its anything different, you'll have to come back and ask again!
Copy linkTweet thisAlerts:
@chelle22authorNov 05.2004 — Thank you guys very much! it works now..I appreciate all your help ?

-michelle
Copy linkTweet thisAlerts:
@senshiNov 05.2004 — np, I was rushing but forgot to tidy up my coding and forgot to change this from var repCount, roll1, roll2, totals = 0;

to

var repCount=totals=0;

which initalizes the variables in one go. the roll1 & roll2 are no longer needed
Copy linkTweet thisAlerts:
@chelle22authorNov 05.2004 — hi again

I'm sorry to bother you. but something seems

wrong with my code again, can you please check it out again?

thanks

________________________________________________________________

<html>

<!-- avgr.html Dave Reed -->

<!-- This page simulates dice rolls and computes total averages -->

<!--------------------------------------------------------->

<head>

<title> Averages </title>

<script type="text/javascript"

src="http://www.prenhall.com/reed/random.js">

</script>

<script type ="text/javascript">

function rollDie(){return Math.ceil(Math.random() * 6);}


var repCount=totals=0;

function roll(numOfRolls)

{

for(i=0; i < numOfRolls; i++){

totals+=rollDie();

var avg = totals / numOfRolls;

document.form1.average.value=avg;

}

repCount = 0;
while (repCount <numOfRolls) { // repeatedly:
roll1 = RandomInt(1, 6); // simulate the dice rolls
roll2 = RandomInt(1, 6);


}

repCount = repCount + 1; // incr. repetition counter

}

</script>

</head>

<body>

<form name="form1">

Desired number of rolls =

<input type="text" name="reps" size=6 value=1000 />

<br /><br />

Average =

<input type="text" name="average" size=6 value=0 />

<br /><br />

<input type="button" name="button" value="Roll Me" onclick="roll;">

<br /><br />

</form>

</body>

</html>
Copy linkTweet thisAlerts:
@senshiNov 06.2004 — Here is a working copy using your RandomInt(1, 6) function but I have added a new dimention to the code so you can see the number of hits each number gets, this can easily be edited out if not needed.

You do need to be more careful with your code and the way that your editing it, the code below functions, read through it ans if your stuk, ask, will run you through it.

<html>

<!-- avgr.html Dave Reed -->

<!-- This page simulates dice rolls and computes total averages -->

<!--------------------------------------------------------->

<head>

<title> Averages </title>

<script type="text/javascript"

src="scripts/random.js"></script>

<script type ="text/javascript">

function roll(numOfRolls)

{

// Initalize function variables and a history array

var totals=r=0, rollHist=new Array(0,0,0,0,0,0);

for(i=0; i < numOfRolls; i++)

{

r=RandomInt(1, 6);

rollHist[r-1]++; // rollHist[r] value is incremented

totals+=r; // add the value or r to the totals

} // end of for loop

var avg = totals / numOfRolls; // calc the average from the total

document.form1.average.value=avg; // update the text box

// update the roll hits history display

for(c=0; c<6; c++)

document.form1.rollHistory[c].value=rollHist[c];

} // end of function roll

</script>

</head>

<body>

<form name="form1">

Desired number of rolls =

<input type="text" name="reps" size="6" value="1000" />

<br><br>

Average =

<input type="text" name="average" size="6" value="0" />

<br><br>

<input type="button" name="button" value="Roll Me" onclick="roll(document.form1.reps.value-0);">

<br><br>Total of rolled numbers:-<br>

No. Hits.<br>

1. <input type="text" name="rollHistory" size="6" value="0" /><br>

2. <input type="text" name="rollHistory" size="6" value="0" /><br>

3. <input type="text" name="rollHistory" size="6" value="0" /><br>

4. <input type="text" name="rollHistory" size="6" value="0" /><br>

5. <input type="text" name="rollHistory" size="6" value="0" /><br>

6. <input type="text" name="rollHistory" size="6" value="0" /><br>

</form>

</body>

</html>
[/QUOTE]
Copy linkTweet thisAlerts:
@chelle22authorNov 06.2004 — thank you very much for all your help ?

-michelle
×

Success!

Help @chelle22 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...