1. Member
Join Date
Oct 2008
Posts
8
Rep Power
0

Random number generater

Hey all,

I'm new to the forums, been using Java for a few months now. I'm still a little blind on using some classes correctly in the way I want it. I have this project based on the game Lets Make a Deal, and my role for my class (java class not school class) is to eliminate one door that isnt the contestant chosen door or the door that the prize is in. I've figured out the all other cases except if the prize door and the contestants chosen door are the same. I was hoping I could use the .random class to choose a door that doesnt match that door. Thing is, if the prize door is door 2 how do I get it to randomly pick between door 1 or door 3. I'm almost finished, Im just hung up on this one piece of code lol. Any input would be greatly appreciated.

2. Senior Member
Join Date
Aug 2008
Posts
384
Rep Power
9
Java Code:
door = (Math.random()<.5?1:3);
Using the ternary operator. If Math.random() returns a number < .5 it's 1, else it's 3. (Math.random() returns a number from 0 to 1

3. hhhmmm... interesting

I did something sorta similiar (notice I said sorta :-), in which I didn't want to repeat a random generated number. I used an array and filled it up (with x's, but anything could be used) as the different random numbers were generated. But before using number I would check to see if the number had been generated before by consulting the array. If it had been generated before I would go back and generate another random number. If It hadn't been generated, fill in the appropiate array element and go use the number. In your case it would be:

random number = 3
before: myArray = [ , ,] (can be used because 3 hasn't been generated before)
after: myArray = [ , ,x]
-------------------
random number = 1
before: myArray = [, ,x] (can be used becuase 1 hasn't been generated before)
after: myArray = [x, ,x]
------------------
random number = 3
before: myArray = [x, ,x] (can't use because 3 has already been generated)
after: myArray = [x, ,x]
-----------------
random number = 2
before: myArray = [x,,x] (can be used because 2 hasn't been generated before)
after: myArray = [x,x,x]

Hope this helps,
CJSL

4. You also use the Random class...

You also use the Random class:

Random randNum = new Random();
myRandomNum= randNum.nextInt(n);//Returns an int value between 0 (inclusive) and the specified value (exclusive)

CJSL

5. Member
Join Date
Oct 2008
Posts
8
Rep Power
0
Thanks guys,

I havent quite learned arrays yet, still working my way up in this class. My professor is taking it pretty slow which is good and bad lol but that suggestion looks like something i should note for the future :) and Supa im going to give your suggestion a try, thanks a lot. Thing that really sucks is my group is assigned the task and the three of us have to each build a class to incorporate into one main method, so its kinda hard to test whether I got this right : / I'm the game show host, one member is the contestant class and the other is handling the doors. Doesnt help that my group is lazy and I'm going to end up having to write all the classes including the main...fuck my life. Haha but thanks for the ideas, its a kick in the right direction!

JOptionPane.showMessageDialog(null, "Thanks!");

6. Member
Join Date
Oct 2008
Posts
8
Rep Power
0
Ooo I was shooting for the random class.

Random door = new Random();

//Door my class has to eliminate.
hostDoor = door.nextInt(2);

Thats what I was going for, but if the prize door and the door the contestant chooses are the same, I have to randomly pick one of the other two doors (door 0, 1, 2). If the prize door and contestant door are the same like door 0, I need it to randomly pick door 1 or 2. My brain hurts because she never went over random class :( and if prize and contestant are door 1, my class has to choose randomly door 0 or 2.

7. OK... here's something I just thought of

Maybe you could do something with three boolean variables:
• boolean bDoor1,bDoor2,bDoor3 = false;

once you know the prize door, just set the cooresponding boolean variable to true.
Use the same logic as before:
• Generate random number
• check against boolean variables
• if false, can be used/assigned
• if true, generate another random number

CJSL

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•