The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Random Number Generator, Trying to work out how to make a random number generator for wikidot,
Stryker
post Jan 31 2015, 11:24 AM
Post #1





Group: Members
Posts: 3
Joined: 31-January 15
Member No.: 22,123



Hi all,

So I've been scouring the internet for months trying to work this one out, but it's utterly eluding me. Here's what I want: I'm trying to create a random number generator to simulate the roll of 2 six sided dice (the bell curve this generates being important), plus or minus a (preset) variable number. I don't want fancy containers or roll buttons, or graphics. Just live text that I can put inline in a paragraph, that will generate a new number with every page refresh. I'm planning on deploying it on Wikidot.

I'm sure someone is going to explain to me why this simply isn't doable, but at the moment it just feels like a simple thing to achieve, and it's a little frustrating that I can't get it working no matter what I try, and being generally pretty code-illiterate, so just trying to cobble together my own functionality based on reading forums and other premade assets, I haven't been able to achieve something this (seemingly) simplistic.

Any and all help would be ENORMOUSLY appreciated.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jan 31 2015, 11:50 AM
Post #2


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



Both javascript and most server-side scripting languages (like PHP) should be able to do that.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Jan 31 2015, 12:41 PM
Post #3


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,716
Joined: 9-August 06
Member No.: 6



Some promising results.
https://www.google.se/search?q=two+dice+javascript
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Stryker
post Jan 31 2015, 09:36 PM
Post #4





Group: Members
Posts: 3
Joined: 31-January 15
Member No.: 22,123



And I'd be able to use Javascript to generate something that could be formatted into a paragraph?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Stryker
post Jan 31 2015, 10:29 PM
Post #5





Group: Members
Posts: 3
Joined: 31-January 15
Member No.: 22,123



Here's what I have at present:

[
HTML
] <script type="text/javascript"> document.write Math.floor(Math.random()*6) +(Math.random()*6) +11); </script> [
]

Now, there's something to do with a ceiling function to get rid of those decimals, which I can't work out, but am sure I can find somewhere online. However getting it to work within other text is proving impossible - if anything precedes it on a line it simple turns the code into live text. Fixable?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Feb 1 2015, 08:01 AM
Post #6


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



QUOTE(Stryker @ Feb 1 2015, 04:29 AM) *

Now, there's something to do with a ceiling function to get rid of those decimals

No, Math.floor() already does that.

QUOTE
However getting it to work within other text is proving impossible - if anything precedes it on a line it simple turns the code into live text. Fixable?

Can't tell from your code example. But you might do it like this (if you want separate values for each dice):

CODE
<p>
before<script type="text/javascript">document.write(Math.floor(Math.random()*6+1)+', '+Math.floor(Math.random()*6+1));</script>after
</p>

Note that Math.random produces numbers from 0 and up to but not including 1, and Math.floor rounds the value down to the nearest integer, so you both need to multiply Math.random with 6 to get values 0-5.99... (that are then rounded down by Math.floor to values 0-5) and then add 1 to get values 1-6.

If you instead want the dice values combined into one you might do it like this:

CODE
document.write(Math.floor(Math.random()*11+2));

--which returns random numbers 0-10.99..., that are rounded down to 0-10, after which you can add 2 to get numbers 2-12.

https://developer.mozilla.org/en-US/docs/We...cts/Math/random
https://developer.mozilla.org/en-US/docs/We...ects/Math/floor
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Feb 1 2015, 11:18 AM
Post #7


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



QUOTE

If you instead want the dice values combined into one you might do it like this:

CODE
document.write(Math.floor(Math.random()*11+2));

--which returns random numbers 0-10.99..., that are rounded down to 0-10, after which you can add 2 to get numbers 2-12.


Yes, but "random numbers from 2 - 12" does not simulate rolling two dice. For example, there is one way of getting 2 (1+1), but 3 ways of getting 4 (1+3, 2+2, 3+1).
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Feb 1 2015, 11:26 AM
Post #8


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



QUOTE(Brian Chandler @ Feb 1 2015, 05:18 PM) *

QUOTE

If you instead want the dice values combined into one you might do it like this:

CODE
document.write(Math.floor(Math.random()*11+2));

--which returns random numbers 0-10.99..., that are rounded down to 0-10, after which you can add 2 to get numbers 2-12.


Yes, but "random numbers from 2 - 12" does not simulate rolling two dice. For example, there is one way of getting 2 (1+1), but 3 ways of getting 4 (1+3, 2+2, 3+1).

In that case you might use separate values, like in the first example. But if the OP is only interested in the combined value it shouldn't matter how the dice arrive at it.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post Feb 1 2015, 12:49 PM
Post #9


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



Well, the OP said:

QUOTE

...a random number generator to simulate the roll of 2 six sided dice (the bell curve this generates being important)


It's hard to see how this could be more specific; the OP is asking about getting a simulation of two throws. A random number from 2-11 does not do this. The first example prints two numbers with a "+" sign between them; you might just as well add them together, and this will indeed be a simulation of the desired type.

Well, perhaps the OP understands this already, judging by the "bell curve" comment.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Darin McGrew
post Feb 1 2015, 01:42 PM
Post #10


WDG Member
********

Group: Root Admin
Posts: 8,365
Joined: 4-August 06
From: Mountain View, CA
Member No.: 3



Note also that there is a (slight) difference between

Math.floor(Math.random()*6+1) + Math.floor(Math.random()*6+1)

and

Math.floor((Math.random()+Math.random())*6+2)

Both will generate bell curves, but they'll be slightly different bell curves. The first represents 2 dice accurately. The second skews the results slightly in favor of higher values. To see the difference, consider

Math.floor(2.8) + Math.floor(3.7) = 2 + 3 = 5
Math.floor(2.8 + 3.7) = Math.floor(6.5) = 6
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Feb 1 2015, 02:27 PM
Post #11


.
********

Group: WDG Moderators
Posts: 9,630
Joined: 10-August 06
Member No.: 7



QUOTE(Brian Chandler @ Feb 1 2015, 06:49 PM) *

It's hard to see how this could be more specific; the OP is asking about getting a simulation of two throws.

I read it as a single throw with two dice, not two single-dice throws.

QUOTE
The first example prints two numbers with a "+" sign between them;

No, it's a comma-separated value pair. Not sure how to generate a bell curve from that, maybe a scatter plot is more suitable?
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 29th March 2024 - 08:32 PM