Guest K1u Posted September 25, 2006 Share Posted September 25, 2006 //******************************************************** // 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 More sharing options...
Guest K1u Posted September 26, 2006 Share Posted September 26, 2006 Please feel free to answer anytime i have to turn this in tommorow lol Link to comment Share on other sites More sharing options...
Guest Posted September 26, 2006 Share Posted September 26, 2006 //******************************************************** // 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 More sharing options...
Guest K1u Posted September 26, 2006 Share Posted September 26, 2006 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 More sharing options...
Guest K1u Posted September 26, 2006 Share Posted September 26, 2006 lol dood i meant keep dice 1 and 2 the same lol its alright u tried Link to comment Share on other sites More sharing options...
Guest Posted September 26, 2006 Share Posted September 26, 2006 It does keep them the same If you random dice 1, then you assign dice2 with the value in dice1 its the same value. Like i said there is no point to it. Link to comment Share on other sites More sharing options...
armadaender Posted September 26, 2006 Share Posted September 26, 2006 I wrote one of this in vb back in high school. Wonder if I saved the source? Link to comment Share on other sites More sharing options...
FFE Posted September 26, 2006 Share Posted September 26, 2006 I had a similar program for my 2nd java. But the teacher wanted 2 random dice added together together then display output. This later turned into a craps game Link to comment Share on other sites More sharing options...
cooper Posted September 26, 2006 Share Posted September 26, 2006 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 More sharing options...
Guest Posted September 26, 2006 Share Posted September 26, 2006 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 More sharing options...
cooper Posted September 26, 2006 Share Posted September 26, 2006 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 More sharing options...
Guest K1u Posted September 26, 2006 Share Posted September 26, 2006 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 More sharing options...
Guest K1u Posted September 26, 2006 Share Posted September 26, 2006 i still dont see the point to doing this. AS I SAID BEFORE IT WAS A SCHOOL ASSIGNMENT geez dood. Link to comment Share on other sites More sharing options...
Guest Posted September 26, 2006 Share Posted September 26, 2006 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 More sharing options...
Freakish Posted September 27, 2006 Share Posted September 27, 2006 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 More sharing options...
Guest Posted September 27, 2006 Share Posted September 27, 2006 1. variable dice 2 = variable dice 12. 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. Link to comment Share on other sites More sharing options...
Freakish Posted September 27, 2006 Share Posted September 27, 2006 Could always just ask the teacher for help. Link to comment Share on other sites More sharing options...
cooper Posted September 27, 2006 Share Posted September 27, 2006 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 More sharing options...
Guest K1u Posted September 28, 2006 Share Posted September 28, 2006 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 More sharing options...
Guest K1u Posted September 28, 2006 Share Posted September 28, 2006 Now freakish dont get mad from the above post im not a angry person but when someone calls me a name thats insulting i get angry. Link to comment Share on other sites More sharing options...
Freakish Posted September 28, 2006 Share Posted September 28, 2006 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 More sharing options...
Ebola Eater of Packets Posted September 28, 2006 Share Posted September 28, 2006 A simple ending to my rant ~PWNED~ QFT Ahem. Seriously, if you're asking for help, don't be a dick. I'm been a member of this forum for a few hours and I know that, ok? Link to comment Share on other sites More sharing options...
VaKo Posted September 28, 2006 Share Posted September 28, 2006 Yay! Another interesting thread down the crapper... locked. Link to comment Share on other sites More sharing options...
moonlit Posted September 28, 2006 Share Posted September 28, 2006 Dude... ;) Link to comment Share on other sites More sharing options...
Recommended Posts