Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default 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. #2
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    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
    I die a little on the inside...
    Every time I get shot.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  5. #5
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default

    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. #6
    Join Date
    Oct 2008
    Posts
    8
    Rep Power
    0

    Default

    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. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default 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
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Random number help
    By jgonzalez14 in forum New To Java
    Replies: 5
    Last Post: 09-16-2008, 09:13 AM
  2. trying to add up random numbers into one number
    By pjr5043 in forum New To Java
    Replies: 4
    Last Post: 09-15-2008, 02:20 PM
  3. Random number
    By jithan in forum Advanced Java
    Replies: 1
    Last Post: 06-13-2008, 01:42 PM
  4. Generate a random number
    By romina in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:23 AM
  5. How to generate random number in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 07:32 PM

Posting Permissions

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