Java statement which generates a random number

Assignment Help JAVA Programming
Reference no: EM132430669

To roll the dice, you can use the following Java statement which generates a random number between 1 and 6 (to represent the six sides of the dice).

die1 = (int)(Math.random()*6)+1

This statement has some interesting characteristics that need to be explained. The first thing we notice is that we are assigning something to a variable with the name die1. You will need to execute this statement twice, once for a variable die1 and the second time to assign to variable die2 to represent the roll of two dice.

The second thing we notice about this is this weird (int) placed in front of the parentheses that contain a subroutine call and a math expression (Math.random()*6).

What the (int) in this case is doing is called a cast. The cast is a way to force java to convert data from one data type to another.

The subroutine (actually this is calling the class Math and the method random) Math.random() will return a number that is between 0 and 1. This number is a floating point number.

By multiplying it by the number 6 we will get a random number between 0 and 5. This number will be a floating point number and is likely to be a fraction meaning that it will have digits to the right of the decimal point.

What the cast or (int) in this case does is convert this number into an integer. As you recall from earlier discussions, this will truncate all the digits to the right of the decimal point. For example assume that the Math.random() subroutine returns the value of .523432. This value is then multiplied by 6 resulting in 3.14059 to this we add 1 resulting in 4.14059. The reason that we are adding a 1 is that we want a number between 1 and 6 starting at 1. By adding a 1 we know that the starting number will always be 1 or greater.

What the (int) will do is to truncate everything to the right of the decimal point and then assign it to the variable die1. The resulting value in die1, in this case, would be 4.

An if statement that will test for 'snake eyes' would be as follows.

if (die1 == 1 && die2 == 1) {

                // do something

}}

You must complete program, test, debug, and execute it. You must submit your java code file. Post the output of your program by specifying the number of rolls of the dice before you got snake eyes. Run the program at least five times and report your results.

See if you can modify your program using a for loop to simulate running your program 1000 times and calculate the average number of rolls required to get snake eyes ... what is the average number of rolls?

Reference no: EM132430669

Questions Cloud

Input-output-validation-functions-loops and decisions : Input, Output, Validation, Functions, Loops and Decisions You will need to have 1 functions, one or more arrays, one or more loops.
Design and implement the game of pig : Design and implement the game of Pig. In this game, the user completes against the computer.
What are the seven steps of doing sociological research : Is it generalizable, and if so how can it be useful to others? Do you see any application to your own life?Are there any ethical dilemmas inherent in research
What specifically happened in the debate : How effective do you believe you were in communicating your position using arguments and evidence from the literature? What specifically happened in the debate.
Java statement which generates a random number : To roll the dice, you can use the following Java statement which generates a random number between 1 and 6 (to represent the six sides of the dice).
Indigenous pigs in oklahoma panhandle : With formal Pig Latin (favored by the indigenous pigs in Oklahoma's panhandle), words that begin with vowels are special. If a word begins with a vowel
Describe cultural components influence their mental health : Identify which relevant subsections of the CFI you used. Explain why you did (or did not) use a subsection as part of your assessment.
Explain ethical implications of the research and findings : Explain the ethical implications of the research and findings. Communicate in a manner that is scholarly, professional, and consistent with expectations.
Try to mimic the output format precisely : Given the below program skeleton. Replace the items with appropriate code so that the program produces the output shown below.

Reviews

Write a Review

JAVA Programming Questions & Answers

  Recursive factorial program

Write a class Array that encapsulates an array and provides bounds-checked access. Create a recursive factorial program that prompts the user for an integer N and writes out a series of equations representing the calculation of N!.

  Hunt the wumpus game

Reprot on Hunt the Wumpus Game has Source Code listing, screen captures and UML design here and also, may include Javadoc source here.

  Create a gui interface

Create GUI Interface in java programing with these function: Sort by last name and print all employees info, Sort by job title and print all employees info, Sort by weekly salary and print all employees info, search by job title and print that emp..

  Plot pois on a graph

Write a JAVA program that would get the locations of all the POIs from the file and plot them on a map.

  Write a university grading system in java

University grading system maintains number of tables to store, retrieve and manipulate student marks. Write a JAVA program that would simulate a number of cars.

  Wolves and sheep: design a game

This project is designed a game in java. you choose whether you'd like to write a wolf or a sheep agent. Then, you are assigned to either a "sheep" or a "wolf" team.

  Build a graphical user interface for displaying the image

Build a graphical user interface for displaying the image groups (= cluster) in JMJRST. Design and implement using a Swing interface.

  Determine the day of the week for new year''s day

This assignment contains a java project. Project evaluates the day of the week for New Year's Day.

  Write a java windowed application

Write a Java windowed application to do online quiz on general knowledge and the application also displays the quiz result.

  Input pairs of natural numbers

Java program to input pairs of natural numbers.

  Create classes implement java interface

Interface that contains a generic type. Create two classes that implement this interface.

  Java class, array, link list , generic class

These 14 questions covers java class, Array, link list , generic class.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd