java's random doesnt like me!

problem with random class

Page 1 of 1

6 Replies - 1189 Views - Last Post: 27 September 2008 - 11:25 AM Rate Topic: -----

#1 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2495
  • View blog
  • Posts: 15,453
  • Joined: 24-September 08

java's random doesnt like me!

Post icon  Posted 26 September 2008 - 09:21 AM

i read ahead in my jaba book during class (cuz i never listen to the teacher :P) and finally found the random class!!! i was so excited (since im trying to work on a video game rpg stye) and decided to make my first little random program that i make in every language when i first discover random: rock, paper, scissors. only, it seems like the random is only picking the same number!!!! gack!! o.< whats wrong with the code? (i know, it very primative, but im going to add other stuff like score and insert everything into a giant while loop (while they still want to play))

package rockpaperscissors;

/**
 *BALLS!!!!!!!!
 * 
 * @author Erica
 */

import javax.swing.JOptionPane;
import java.util.Random;

public class Main {

    public static void main(String[] args) {
       
        String input;                     //for JOP
        int uchoice;                      //parsed input
        int compchoice;                //random number choice from compy

        //prompts user for choice and parses it to uchoice
        input = JOptionPane.showInputDialog("What'll it be? Rock, paper, or scissors?\n" +
                "1 for rock, 2 for paper, and 3 for scissors: ");
        uchoice = Integer.parseInt(input);
        
        //assigns a random number to randomnum 0-2? and assigns it to compchoice
        Random randomnum = new Random ();
        compchoice = randomnum.nextInt(2);
        
    
        //determines if the user won or not and then tells them so
        if (uchoice == 1 && compchoice == 0)
                JOptionPane.showMessageDialog(null, "Tie!");
        
        else if (uchoice == 1 && compchoice == 1)
                JOptionPane.showMessageDialog(null, "Paper beats rock. You lose!");
        
        else if (uchoice == 1 && compchoice == 2)
                JOptionPane.showMessageDialog(null, "Rock beats scissors. You win!");
        
        else if (uchoice == 2 && compchoice == 0)
                JOptionPane.showMessageDialog(null, "Paper beats rock. You win!");
        
        else if (uchoice == 2 && compchoice == 1)
                JOptionPane.showMessageDialog(null, "Tie!");
        
        else if (uchoice == 2 && compchoice == 2)
                JOptionPane.showMessageDialog(null, "Scissors beats paper. You lose!");
        
        else if (uchoice == 3 && compchoice == 0)
                JOptionPane.showMessageDialog(null, "Rock beats Scissors. You lose!");
        
        else if (uchoice == 3 && compchoice == 1)
                JOptionPane.showMessageDialog(null, "Scissors beats paper. You win!");
        
        else if (uchoice == 3 && compchoice == 2)
                JOptionPane.showMessageDialog(null, "Tie!");
        
    }
    

}



Mod Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Is This A Good Question/Topic? 0
  • +

Replies To: java's random doesnt like me!

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: java's random doesnt like me!

Posted 26 September 2008 - 09:45 AM

You want nextInt(3); not (2). 2 means you get 2 number choices.

nextInt(3); // 0-3 (never 3)
nextInt(2); // 0-2 (never 2)


Hope this helps!
Was This Post Helpful? 0
  • +
  • -

#3 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2495
  • View blog
  • Posts: 15,453
  • Joined: 24-September 08

Re: java's random doesnt like me!

Posted 26 September 2008 - 10:27 AM

so youre saying if i use nextInt(3), 3 can never be the number, or 0?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: java's random doesnt like me!

Posted 26 September 2008 - 01:50 PM

View PostNeoTifa, on 26 Sep, 2008 - 10:27 AM, said:

so youre saying if i use nextInt(3), 3 can never be the number, or 0?


random.nectInt(n) returns an it which is >= 0 and < n
so
random.nextInt(2) returns 0 or 1
you want
random.nextInt(3) which will return 0, 1 or 2
Was This Post Helpful? 0
  • +
  • -

#5 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1376
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: java's random doesnt like me!

Posted 26 September 2008 - 02:48 PM

View PostNeoTifa, on 26 Sep, 2008 - 05:21 PM, said:

jaba


Posted Image

Couldn't resist.
Was This Post Helpful? 0
  • +
  • -

#6 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: java's random doesnt like me!

Posted 26 September 2008 - 07:38 PM

View Postgabehabe, on 26 Sep, 2008 - 02:48 PM, said:

View PostNeoTifa, on 26 Sep, 2008 - 05:21 PM, said:

jaba


Posted Image

Couldn't resist.


uh-wha?

View Postpbl, on 26 Sep, 2008 - 01:50 PM, said:

View PostNeoTifa, on 26 Sep, 2008 - 10:27 AM, said:

so youre saying if i use nextInt(3), 3 can never be the number, or 0?


random.nectInt(n) returns an it which is >= 0 and < n
so
random.nextInt(2) returns 0 or 1
you want
random.nextInt(3) which will return 0, 1 or 2


Precisely.

This post has been edited by Locke37: 26 September 2008 - 07:39 PM

Was This Post Helpful? 0
  • +
  • -

#7 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2495
  • View blog
  • Posts: 15,453
  • Joined: 24-September 08

Re: java's random doesnt like me!

Posted 27 September 2008 - 11:25 AM

oh, ok thnx :)

edit: i just ran it, it works wondefully now thnx. now to add some other features....^_^

This post has been edited by NeoTifa: 27 September 2008 - 11:28 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1