Code Monkey home page Code Monkey logo

rolldie's Introduction

rollDie

This is a challenge for a class that I am interested in attending.

Problem

Roll a number 1-10 from a 6-sided die.

Soultion

I thought of two solutions so I coded both of them. If I chose one, I would go with solution A since it requires fewer rolls on average.

Solution A

Divide the numbers 1-10 in half. Roll the die. If 1, 2, or 3 is rolled, it represents the numbers 1-5. If 4, 5, or 6 is rolled, it represents the numbers 6-10. Roll the die again until you get a number that isn't 6. If the first roll was less than or equal to 3 then return the number (which will be 1, 2, 3, 4, or 5), if the first roll was greater than 3, then return the number plus 5 (1+5=6, 2+5=7, 3+5=8, 4+5=9, or 5+5=10). This on average takes a little over 2 rolls.

Solution B

Roll the die 5 times. Rolling the 6-sided die 5 times because the least common multiple of 6 and 10 is 30 (6x5=3x10). Sum the results of each roll and modulo the sum by 10. Add one to the remainder and return the result. The modulo values are evenly distributed as well as the 6 sides of the dice (because of the LCM characteristic), so the results should be equally probable.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.