#Roll a dice with random generator.

1 messages · Page 1 of 1 (latest)

torpid bramble
#

This is the example we were given.
Write a Java class that simulates rolling dice. The
class should contain two methods, roll and main, as follows:
❑ int roll() – This method should use the Random class
from the Java library to simulate rolling a die by generating a
random number. It should return the value rolled, an integer
from 1 to 6 (don’t print here) [Look up Random in the Java docs!]
❑ main – This method should simulate rolling the die 20 times.
It should call the roll method 20 times and print out each roll.
It should also calculate & print the average roll as a double.
❑ Do not make the roll method static
▪ Create an object variable of your class in the main method to call the roll method.

I know that this should be so simple but I am struggling with it so bad.

This is what I have so far and when I run it, it just prints the number 5 once. Im not sure what is going on here because even when I change the range of the random method, only a 5 prints out.
public class DieRoll {
public int roll(int randomNumber){
for (int i = 0; i < 20; i++){
randomNumber =(int)(Math.random()* 6)+ 1;
}
return randomNumber;
}

public static void main(String[] args) {

   DieRoll dieOne = new DieRoll();
   dieOne.roll(20);
}

}

full shoreBOT
#

<@&987246399047479336> please have a look, thanks.

full shoreBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

azure juniper
#

The for loop should be in main. You're only calling the method once now, not 20 times

#

also not sure where youre getting a print from. there is no print statement in that code

torpid bramble
#

I am not sure where its getting the 5 from either. I've moved the for loop and am still getting the same result

viscid bay
#

Btw. it tells you to use Random random = new Random(); instead of the Math method.

#

On random you can also call nextInt() which spares you the casting.

#

The proper way would be to use ThreadLocalRandom.current().nextInt() but I assume you're not allowed to do that.

#

Because it's bad practice to create own random objects, when there is already one present on the thread. But that's how your requirements are.

#

@torpid bramble ^

rancid bane
#

it also depend if you want control on the randoms

viscid bay
#

Yeah, in special cases maybe. But not when rolling a dice.

rancid bane
#

seeds are more important that you may think

viscid bay
#

You mean like, when giving out prices?

rancid bane
#

When you want to reproduce something

viscid bay
#

That might be a point.

rancid bane
#

let's say you have a bug with your random code

#

you can't reproduce it without a seed

#

but if you use a seed

#

you can simply use it again

viscid bay
#

Yeah, that's true.

torpid bramble
#

thank you guys for trying to help! I wish any of those things helped but unfortunately did not.

viscid bay
#

Np. So you might give us your updated code and tell us what problem it has.

torpid bramble
#

before I try switching to the random class I'm just trying to get it to work with the random method. I can post what I have now but its kind of all over the place. I keep googling things and they just get me more lost.

#

public class DieRoll {
public int roll(int randomNumber){
randomNumber =(int)(Math.random()* 6)+ 1;
return randomNumber;
}

public static void main(String[] args) {
   DieRoll dieOne = new DieRoll();
   int s =0;
   for (int i = 0; i < 20; i++){
       System.out.println(dieOne.roll(s));
   }
}

}

#

❑ Write a Java class that simulates rolling dice. The
class should contain two methods, roll and main, as follows:
❑ int roll() – This method should use the Random class
from the Java library to simulate rolling a die by generating a
random number. It should return the value rolled, an integer
from 1 to 6 (don’t print here) [Look up Random in the Java docs!]
❑ main – This method should simulate rolling the die 20 times.
It should call the roll method 20 times and print out each roll.
It should also calculate & print the average roll as a double.
❑ Do not make the roll method static
▪ Create an object variable of your class in the main method to call the roll

#

I think I have the roll method correct following specifications but its the main method and the object that im confused with

azure juniper
#

why are you giving roll a number?

#

you dont do anything with it

#

also "i am confused with" is not a question 😛

torpid bramble
#

The requirements say to use the roll method to generate a random number. Is that not what I'm doing there? I believe I'm getting thrown off with the object part. What exactly does creating an object do in this case? I'm not understanding how to use the object to get the value

karmic frigate
#

ur usage of the object is okay. but u have to realize that ur method doesn't need input

#

method parameters mean "give me something"

#

ur method doesnt need any input

#

it can produce and return an output without any input

#

that said, ur conflicting ur task which says ur supposed to use Random

#

and not Math.random()

#

think of the object as the dice, which u can roll

azure juniper
# torpid bramble The requirements say to use the roll method to generate a random number. Is that...

i think you are confused about the Object part because you could have done the same thing without making the object, and instead using a static function.
the thing is, you properly implemented the "object part" into your code!

early Object Oriented Programming assignments/classes can be confusing because it often involves making and using objects when you really do not need any. this will hopefully solve itself once your classes get more complicated 🙂

torpid bramble
#

Ahhh okay that helps a bunch, yes these assignments have been so confusing to me and my prof keeps saying I'm over thinking it too much lol

karmic frigate
#

having objects becomes interesting when the dice would maintain and remember their current face

#

then u could create 5 dice and roll all of them and then maybe reroll 3 of them and still being able to ask them for their current face

#

so when they start to remember something and when u need multiple of them

torpid bramble
#

im going to need to get a tutor lol thank you guys for all your help though!!!