Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 11th Nov 2014, 09:26 PM   #1
Warrior Member
 
Join Date: 2014
Posts: 21
Thanks: 5
Thanked 0 Times in 0 Posts
Default
Trying to get dice generator script
Share on: 
fb share twitter share gplus share more share

Hi,

I'm having real difficulties getting a dice generator onto my website.
Does anyone know a way to do it?

I've looked into the Javascript but so far the code I'm trying isn't working.

Has anyone got ideas that can help me?
Tyler Danann is offline  
Unread 11th Nov 2014, 10:03 PM   #2
HyperActive Warrior
War Room Member
 
emptee's Avatar
 
Join Date: 2014
Location: New Zealand
Posts: 210
Thanks: 25
Thanked 81 Times in 71 Posts
Default
Re: Trying to get dice generator script
Share on: 
fb share twitter share gplus share more share

Hi Tyler,

Post the code (and any relevant html) and I'll take a look.

Cheers,
Michael

Originally Posted by Tyler Danann View Post

Hi,

I'm having real difficulties getting a dice generator onto my website.
Does anyone know a way to do it?

I've looked into the Javascript but so far the code I'm trying isn't working.

Has anyone got ideas that can help me?

emptee is offline  
Unread 11th Nov 2014, 10:22 PM   #3
Warrior Member
 
Join Date: 2014
Posts: 21
Thanks: 5
Thanked 0 Times in 0 Posts
Default
Re: Trying to get dice generator script
Share on: 
fb share twitter share gplus share more share

<script language="JavaScript1.1" src="DiceQuote1.js">

</script>
<form>
<INPUT TYPE="button" onClick="history.go(0)" VALUE="Roll Dice">

</p>

</form>

External Link Script:

// JavaScript Document

var randomnumber=Math.floor(Math.random()*12)

//change the quotes if desired. Add/ delete additional quotes as desired.

quotes[0]='1'

quotes[1]='2'

quotes[2]='3'

quotes[3]='4'

quotes[4]='5'

quotes[5]='6'

quotes[6]='7'

quotes[7]='8'

quotes[8]='9'

quotes[9]='10'

quotes[10]='11'

quotes[11]='12'

var whichquote=Math.floor(Math.random()*(quotes.length ))
document.write(quotes[whichquote])

So basically I've got the form buttons to refresh the page hopefully chucking out a random number on the screen each time.
Tyler Danann is offline  
Unread 11th Nov 2014, 10:29 PM   #4
HyperActive Warrior
War Room Member
 
emptee's Avatar
 
Join Date: 2014
Location: New Zealand
Posts: 210
Thanks: 25
Thanked 81 Times in 71 Posts
Default
Re: Trying to get dice generator script
Share on: 
fb share twitter share gplus share more share

Try this instead:

Code:
var randomnumber=Math.floor(Math.random()*12);

//change the quotes if desired. Add/ delete additional quotes as desired.

var quotes = [];

quotes[0]='1';

quotes[1]='2';

quotes[2]='3';

quotes[3]='4';

quotes[4]='5';

quotes[5]='6';

quotes[6]='7';

quotes[7]='8';

quotes[8]='9';

quotes[9]='10';
quotes[10]='11';

quotes[11]='12';

var whichquote=Math.floor(Math.random()*(quotes.length ));
document.write(quotes[whichquote]);
Originally Posted by Tyler Danann View Post

<script language="JavaScript1.1" src="DiceQuote1.js">

</script>
<form>
<INPUT TYPE="button" onClick="history.go(0)" VALUE="Roll Dice">

</p>

</form>

External Link Script:

// JavaScript Document

var randomnumber=Math.floor(Math.random()*12)

//change the quotes if desired. Add/ delete additional quotes as desired.

quotes[0]='1'

quotes[1]='2'

quotes[2]='3'

quotes[3]='4'

quotes[4]='5'

quotes[5]='6'

quotes[6]='7'

quotes[7]='8'

quotes[8]='9'

quotes[9]='10'

quotes[10]='11'

quotes[11]='12'

var whichquote=Math.floor(Math.random()*(quotes.length ))
document.write(quotes[whichquote])

So basically I've got the form buttons to refresh the page hopefully chucking out a random number on the screen each time.

emptee is offline  
Unread 11th Nov 2014, 10:34 PM   #5
Warrior Member
 
Join Date: 2014
Posts: 21
Thanks: 5
Thanked 0 Times in 0 Posts
Default
Re: Trying to get dice generator script
Share on: 
fb share twitter share gplus share more share

Thanks, I'll try that now mate
Tyler Danann is offline  
Unread 11th Nov 2014, 10:45 PM   #6
Conceited Know-It-All
 
kilgore's Avatar
 
Join Date: 2014
Location: Amurrica
Posts: 952
Thanks: 465
Thanked 1,136 Times in 503 Posts
Default
Re: Trying to get dice generator script
Share on: 
fb share twitter share gplus share more share

The problem with your script is that you're rolling one 12-sided die instead of two 6-sided dice. Maybe it doesn't actually matter for your use case, but if your users are expecting a 7 to appear more than a 2 or a 12, they may be disappointed.

Really you should have two random numbers generated, not just one.

kilgore is offline  
Unread 12th Nov 2014, 06:55 AM   #7
Warrior Member
 
Join Date: 2014
Posts: 21
Thanks: 5
Thanked 0 Times in 0 Posts
Default
Re: Trying to get dice generator script
Share on: 
fb share twitter share gplus share more share

Originally Posted by kilgore View Post

The problem with your script is that you're rolling one 12-sided die instead of two 6-sided dice. Maybe it doesn't actually matter for your use case, but if your users are expecting a 7 to appear more than a 2 or a 12, they may be disappointed.

Really you should have two random numbers generated, not just one.
Hi, Yeah so I guess just coping the code again should do the trick for that?
Tyler Danann is offline  
Unread 16th Nov 2014, 04:28 AM   #8
Active Warrior
War Room Member
 
Join Date: 2012
Posts: 71
Thanks: 2
Thanked 13 Times in 13 Posts
Default
Re: Trying to get dice generator script
Share on: 
fb share twitter share gplus share more share

Here is a bit better way to do it using jQuery.

Code:
<!DOCTYPE html>
<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    </head>
    <body>
        <button id="roll">Roll</button>
        
        <script type="text/javascript">
            $(document).ready(function(){
                var quotes = ["quote 1", "quote 2", "quote 3", "quote 4", "quote 5", "..."];

                $("#roll").click(function(){
                    var random = Math.floor(Math.random() * (quotes.length));
                    alert(quotes[random]);
                });
            });
        </script>
    </body>
</html>

Lani is offline  
Closed Thread


Bookmarks

Tags
dice, generator, script


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 Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 05:21 AM.