Jump to content

Very quick (easy) java problem


Guest K1u
 Share

Recommended Posts

//********************************************************

// Generates 2 random dice rolls and gives total of both.

//

// Author: Kiumarz Hashemi

//********************************************************



import java.util.Random;



public class Dice

{

    public static void main (String[] args)

    {

     Random generator = new Random();

     

     

     int dice1;

     int dice2;

     int total;

     

     

     dice1 = generator.nextInt(6)+ 1;

     System.out.println ("First roll t " + dice1);

     

     dice2 = generator.nextInt(2)+ 1;

     System.out.println ("Second roll t " + dice2);

     

     total = dice1 + dice2;

     System.out.println ("Total tt " + total);

     

     

        

    }

}

Ok very quick question what would i do to make dice1 and dice 2 come out the same number every time without changing the original 6 and 2 so basically every time the random numbers come up dice1 and 2 will be the samebut still random just that 1 and 2 will be same in output.

for example

output

First roll 2

Second roll 2

Total 4

Press any key to continue...

then if i decide to run again get different numbers but still first roll and second roll same

First roll 1

Second roll 1

Total 2

Press any key to continue...

But keeping the 6 and 2 the same in code

feel free to modify the code for this all you want

Link to comment
Share on other sites

//********************************************************

// Generates 2 random dice rolls and gives total of both.

//

// Author: Kiumarz Hashemi

//********************************************************



import java.util.Random;



public class Dice

{

    public static void main (String[] args)

    {

     Random generator = new Random();

     

     

     int dice1;

     int dice2;

     int total;

     

     

     dice1 = generator.nextInt(6)+ 1;

     System.out.println ("First roll t " + dice1);

     

     dice2 = dice1;

     System.out.println ("Second roll t " + dice2);

     

     total = dice1 + dice2;

     System.out.println ("Total tt " + total);

     

     

      

    }

}

But i dont get it, whats the point of having dice 2 if its always going to be the same as dice 1? just times the result of dice1 by two and be done with it :?

Link to comment
Share on other sites

But i dont get it, whats the point of having dice 2 if its always going to be the same as dice 1? just times the result of dice1 by two and be done with it

Yeah i dont either its for school so...yeaaaa...well thanks

Link to comment
Share on other sites

When you create the Random object you can pass it a parameter as a seed to initialise its pseudo-random number generator (PRNG) which is a long. When you don't pass it a parameter, it'll default to using System.currentTimeMillis.

To make the Random object always return the same sequence of numbers, pass it the same long value for initialisation (0 comes to mind).

All PRNGs work this way. Real RNGs don't have/allow this, and for good reason.

Link to comment
Share on other sites

i still dont see the point to doing this. If the value that the random number is based of is hard coded into the app then the random number is going to be same every single time the program is ran, and you still dont need to run the random function on both variables as it will allways return the same value, so just assigning the first run of it to the secound would be the better option as its going to be faster. But if you dont want the value hard coded in, you could use the system clock and pass the value you get from that to both calls of random, and this will make both variables the same. But once again, why do this when you can just assign the first one to the secound. It will be faster then calling the random function twice.

Link to comment
Share on other sites

i still dont see the point to doing this. If the value that the random number is based of is hard coded into the app then the random number is going to be same every single time the program is ran

True. It doesn't make a lot of sense in that regard, but that's what school is for. You don't have to see a practical use of it, you simply need to know that it's possible (so you know what to look out for when reviewing code).

and you still dont need to run the random function on both variables as it will allways return the same value

Um. No, that's not true. The second call will return a different value.

You'll only get the same value for the second call when you recreate a new Random object using the same seed value and pull a new integer off of that.

Link to comment
Share on other sites

When you create the Random object you can pass it a parameter as a seed to initialise its pseudo-random number generator (PRNG) which is a long. When you don't pass it a parameter, it'll default to using System.currentTimeMillis.

To make the Random object always return the same sequence of numbers, pass it the same long value for initialisation (0 comes to mind).

All PRNGs work this way. Real RNGs don't have/allow this, and for good reason.

Exactly i see ur very good at java cooper when 0 is the seed it wont do any random numbers due to that random numbers r generated using the system clock (this is actually true).

Link to comment
Share on other sites

You'll only get the same value for the second call when you recreate a new Random object using the same seed value and pull a new integer off of that.

My bad, thats what i meant, it just didnt come out like that. Even if i was still at school though and got given this i would still do it the way i said. And i would argue the point. Its not like the teacher can mark me down for it. It does what the teachers wants, and is the better solution :roll: but yeh i know what your saying its school and the teacher is just trying to show how you can manipulate the random function. But he could of picked something better to do it with.

Link to comment
Share on other sites

i still dont see the point to doing this.

AS I SAID BEFORE IT WAS A SCHOOL ASSIGNMENT geez dood.

First, if you want help you probably shouldn't be such an asshole.

Second, stop double posting, use the edit button.

Third, it is spelt dude not dood, dood isn't a word http://www.thefreedictionary.com/dood

I don't know java but I do know basic coding logic so there are two things you can probably do.

1. variable dice 2 = variable dice 1

2. Only have one dice but display it twice, then when you go to show the total just multiply it by two.

Also before you go fight me about the things that I said, they are true, and yes I am an asshole.

Oh ya before I forget we are not here to do your homework for you, if I can help you I'll probably try but no one hast to help you.

Link to comment
Share on other sites

1. variable dice 2 = variable dice 1

2. Only have one dice but display it twice, then when you go to show the total just multiply it by two.

Thats the sultion i told her, and got told i was wrong for it. And its still jumping down my throght even though i understand its school work, But just because its school work doesnt mean you shouldnt do the most logical way. Hence dice2 =dice1, is the most logical way, In a real world situation that is how you would do it. So if the teacher marks you down for it argue the point. :wink:

Link to comment
Share on other sites

Hrmm. Seems I omitted part of the original question: The part of dice1 and dice2 coming out as having the same number.

dice1 will be any integer between 1 and 6 inclusive.

dice2 will be any integer between 1 and 2 inclusive.

So the trick is to not only reset the generator (use the setSeed method), but also take the modulo 2 of the nextInt(6) call before adding 1 to it.

So you end up with something like this:

import java.util.Random;

public class Dice {

    private static final long SEED = 0;

    public static void main (String[] args) { 

        Random generator = new Generator(SEED);

        dice1 = generator.nextInt(6) % 2 + 1;

        System.out.println ("First roll t " + dice1);

        generator.setSeed(SEED);

        dice2 = generator.nextInt(2) + 1;

        System.out.println ("Second roll t " + dice2);

        System.out.println ("Total tt " + total);

    }

}

Link to comment
Share on other sites

Oh ya before I forget we are not here to do your homework for you, if I can help you I'll probably try but no one hast to help you.

dood i knew the answer i wanted to share with u all that its interesting how it uses the system clock to generate random numbers and i say dood bcuz i can soo yeah :D ... plus i wanted to see other ways of doing this thing sorry if i was posting some code on a hak5 forum in the coding forum maybe i should not do that and dont call me a asshole again u DONT EVEN KNOW JAVA as u clearly stated plz dood dont talk to me liek that u ignorant fuck ooooh now ur gonna quote me and post statements saying how im this and that well go fuck ur self u worthless cunt... ~PWNED~ (thanks for the help cooper i appreciate it u actually know what ur talking about and know it very well unliek freakish)

Link to comment
Share on other sites

Oh well now I just have to reply seeing how bad I was pwned

~PWNED~
Thank god you put that there or else I wouldn't have known that your bloody massacre of the english language was an insult.

Well lets continue.

Clearly you were asking a question if you look here

Ok very quick question what would i do to make dice1 and dice 2 come out the same number every time without changing the original 6 and 2 so basically every time the random numbers come up dice1 and 2 will be the samebut still random just that 1 and 2 will be same in output.
and here
Please feel free to answer anytime i have to turn this in tommorow lol
and the title of the thread is "Very quick (easy) java problem" so I'm sure that even you can see it.

Oh ya you are an asshole. I call you an asshole because I can. Your also right that I don't even know Java, what next, are you going to tell me that my hair is brown?

I love the irony in you calling me an ignorant fuck, seeing how you don't know me and I didn't say anything ignorant.

Could you please tell me how I "a worthless cunt" can fuck myself, I really don't think that it is possible.

A simple ending to my rant ~PWNED~

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...