Okay, so it's been awhile since I've posted on here, but I am having trouble figuring this problem out. So, students need to be assigned randomly to experiments. Experiment 1 has 5 participants, 2 has 10 participants, and 3 has 20 partcipants. Every iteration through the loop, it asks for a student's name to be typed, then it randomly picks an experiment for that string to be assigned to and displays the student's name and assignment to the console. Can someone please help me out on how to randomly get a number between 1-3?

import java.util.Scanner;
public class psy1113 {

     * @param args
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int experimentA;
        int experimentB;
        int experimentC;
        String name;
        int numberOfParticipants;

    //Create a scanner
    Scanner input = new Scanner(System.in);
    numberOfParticipants = 0;
    int experiment = (int)(Math.random());

    System.out.println("What is the name of the participant?"); 
    name = input.next();
    }while (numberOfParticipants <= 35);

    System.out.println("Here are all the stupid names:" + name);


4 Years
Discussion Span
Last Post by stultuske

Use Random like this

//up at top import java.util.Random; or java.util.*;
Random rand = new Random();
int a = rand.nextInt(3); // will generate 0 - 2

just google "Java roll dice" and you'll find tons and tons of examples of how to find a random number in the reach [1-6], I'm pretty sure you'll be able to adapt it to code that returns a random number in the reach of [1-3]

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.