Does Either Of These Randomly Generate Both 0 And 1?
Nov 22, 2014
"which code is best for randomly generating integer 0 or 1". Haven't gotten my grade back yet but in reading these during the test I didn't think any of these would kick out both of those numbers, but there was no "none of the above" option on the test.This is exactly how it appeared on the test:
A) (int)Math.random() + 1
B) (int)(Math.random() + 0.2)
C) (int)Math.random()
D) (int)(Math.random() + 0.8)
E) (int)(Math.random() + 0.5)
I've tried all of them in the cs lab 100 times each and none of them generated both numbers. 'A' kicked out 1 every time. 'B,D and E' kicked out 0.2, 0.8 and 0.5 respectively each time, and 'C' kicked out 0. Did I just not run them enough times for the result to change or am I right in thinking there's a glitch on the test?
View Replies
ADVERTISEMENT
Sep 25, 2014
1. Write a Java program that randomly generates a five-digit lottery number and prompts the user to enter a five-digit number. Each digit in the number is in the range between 0~9. The program should determine which prize the user wins according to the following rule:
- The user wins the first prize if the user input matches all five digits in the lottery number in exact order.
-The user wins the second prize if the user input matches any four digits in the lottery number in exact positions.
-The user wins the third prize if the user input matches any three digits in the lottery number in its exact position.
-The user wins the fourth prize if the user input matches any two digits in the lottery number in its exact position.
- The user wins the fifth prize if the user input matches any one digit in the lottery number in its exact position.here is my code. I have tried replacing the && statements with || and it always returns either case 1 or case default.
import java.util.Scanner;
import java.util.Random;
class Hw5 {
static int getPrize(int g1, int g2, int g3, int g4, int g5,
int u1, int u2, int u3, int u4, int u5) {
[code]...
View Replies
View Related
Nov 4, 2014
My random integer always seems to be zero.. I am at the ends of my wit.
package Exercises;
import java.util.Random;
import java.util.Scanner;
import javax.swing.JOptionPane;
/**
* Heads or tails?
* That is what this is.
*/
public class num14 {
[Code] ....
Attached File(s) : New Text Document.txt (2.59K)
View Replies
View Related
Dec 13, 2014
So i am making this game with randomly generated labyrinths and i get error in array. Here is the code:
import java.util.Random;
public class Labyrinth {
//0-walls
//1-path
[code]....
I've got lost in some things so my code might contain unnecessary code.
View Replies
View Related
Jan 7, 2015
I created and an Array of integers how can I get randomly get/pick the index of each array element.
View Replies
View Related
Apr 8, 2015
i'm trying to make a random card shuffler but the output would sometimes have same value multiple times. For example it might print out A5 at the fifth index then print out A5 again as the 32 index.
public class randomGenerateCards {
public static void main(String[] args) {
String temp;
String[] cards = new String[]
{ "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10", "A11",
"A12", "A13", "S1", "S2", "S3", "S4", "S5", "S6", "S7", "S8",
"S9", "S10", "S11", "S12", "S13", "H1", "H2", "H3", "H4", "H5",
"H6", "H7", "H8", "H9", "H10", "H11", "H12", "H13", "D1", "D2",
"D3", "D4", "D5", "D6", "D7", "D8", "D9", "D10", "D11", "D12",
[code]....
View Replies
View Related
Oct 19, 2014
So I an assignment in Java to write a code which will randomly populate squares in a Tic Tac Toe Board. I pretty much have it I think, but I cannot get the 'O' to appear on the board, some squares will be blank. We were told to use the Random utility to generate the squares. I am attaching the .gif's which are used. Here is my code:
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class LandryTicTacToe extends JFrame
{
/**
*
*/
private static final long serialVersionUID = -8781512780135301721L;
private final int HEIGHT = 450;//Set value for Height
private final int WIDTH = 500;//Set value for Width
private static JButton [] button = new JButton[9];//Declare array of Buttons
[Code] .....
[attachment=37084:TicTac0.gif][attachment=37085:TicTacX.gif]
Instructions Given to me:
Display a frame that contains nine labels, arranged like a Tic Tac Toe board. A label may display an image icon for X, an image icon for O, or nothing. Display images randomly in each label. Use the Random class to generate 0, 1, or 2, which corresponds to displaying an X image, an O image, or nothing.
Attached image(s)
View Replies
View Related
Jul 5, 2014
I need to make a string filled with naughts and crosses like this one : "xxx ooo xox". There are 3 groups separated with a space. how to fill the string randomly ?
View Replies
View Related
Jan 18, 2014
I want to create a program that takes n number of people (String vector) and assigns them a random group number (int) the range from 1 to k. The number n is evenly divided by k.
I have created a program that associates every String with a random int number. There is however the problem that the random generator just gives a random number, but doesn't make an equal number of people for each group. Which means even if the numbers are random, some of the numbers happen to come up more times than others.
How can I write, to make sure that each number is generated the same amount of times?
View Replies
View Related
Sep 22, 2014
I am making a game and i am trying to have enemies spawn slight off the screen and randomly each time the game is run. I can get them to spawn randomly but i cant seem to get them off the screen properly ( when i do they take too long to enter the screen).
here is the code
for(int b = 0; b < GamePanel.enemy_amount; b++){
GamePanel.enemy.add(new Enemy((int)(Math.random() * 4000),(int)(Math.random() * 200)));
}
here is the width and height of the JFrame
setSize(1200, 800);
View Replies
View Related
Dec 28, 2014
I am trying to program a slot machine and as of now, I am trying to make my slots spin and randomly stop on one of the graphics. Right now, I am having difficultly making the animation/slot stop randomly.
Previously, I tried using a randomly set timer that would cancel the execution, but the image did not appear/stop on the screen.Right now, I am using a random generator and a while to say that if it is a certain number, then stop the image.Here's what I tried:
import java.applet.*;
import java.awt.*;
import hsa.*;
import java.util.Random;
public class SlotGraphics
{
Console c;
int x = 200;
int y = -100;
[code]....
View Replies
View Related
Dec 7, 2014
I must fill a 2d array randomly and then apply methods to the array. However i keep getting an out of bounds exception no matter what dimensions i use. I have a test and a class program.
import java.util.*;
import java.lang.Math;
import java.util.Arrays;
import java.util.Random;
class SummerStats {
Random rand = new Random();
[Code] .....
View Replies
View Related
Sep 1, 2014
In my program I want to 2 images changed randomly regulations, but on frame appear only img4. What is wrong?
public class Yes extends JPanel {
Image img3 = new ImageIcon("Yes/Untitled3.png").getImage();
Image img4 = new ImageIcon("Yes/Untitled4.png").getImage();
Random rand = new Random();
int randomNum = rand.nextInt();
[Code] ....
View Replies
View Related
Jun 15, 2014
I have the code and it works very well, but my professor wants us to use Junit testing to test our code. I've never used JUnit before, how it works. Is it possible to have a boolean value (true or false) randomly set?
Here is the code I need to test:
package musicalinstruments;
class MusicalInstrument {
public String name;
public boolean isPlaying;
public boolean isTuned;
public MusicalInstrument(){
isPlaying = false;
isTuned = false;
[Code]...
View Replies
View Related
Nov 19, 2014
i am trying to print out a randomly generated array, but i only get
[I@7852e922
i did some research and the "[" is for arrays, "I" is for the int and "@" is the hash. and the rest is some sort of hex. I need to override this in a way, but i can't seem to find out how.
this is my current code:
import java.util.Random;
public class Oppgave {
public static void main(String[] args){
int myint[] = fyll();
System.out.println(myint);
}
public static int[] fyll() {
[Code]...
View Replies
View Related
Jun 13, 2014
Now I am trying to print the 4 string randomly from string array..where string contains no of words which are splitted from the file....
View Replies
View Related
Apr 16, 2015
I need figuring this problem out. It appears that I am attempting to generate a permutation of the string "ABCDEF" 720,000 times using this method:
In the second method, j is chosen randomly in the range from 0 to i (inclusive).
Once the permutations are generated, the program will proceed in counting the number of times each permutation occurs, calculating the chi square statistic of the situation, and creating the chi square distribution with 719 degrees of freedom, then outputting the statistic and the chi square probability of the permutations. The generatePermutation method is where all the magic happens. Only trouble, I can't figure out what I equals to. The times where I think I have i as a correct value only give me the program outputted as 1.0 probability every time. What it needs to be doing is outputting variable probability as a number always between 0 and 1, not 1 all the time. Here is my code:
assignment7part2.java:
package math3323assignment7;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import org.apache.commons.math3.distribution.ChiSquaredDistribution;
import com.google.common.collect.Multiset;
import com.google.common.collect.TreeMultiset;
[Code] ....
i is meant to step through the index of the array, but I can't figure out what i is.
View Replies
View Related
Mar 25, 2014
Dice are used in many games. One die can be thrown to randomly show a value from 1 through 6.
Design a Die class that can hold an integer data field for a value (from1 to 6).
Include a constructor that randomly assigns a value to a die object. Appendix D contains information on generating randomnumbers. To fully understand the process, you must learn more about Java classes and methods. However, for now, you can copy the following statement to generate a random number between 1 and 6 and assign it to a variable. Using this statement assumes you have assigned appropriate values to the static constants.
randomValue = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE +
LOWEST_DIE_VALUE);
Also include a method in the class to return a die's value. Save the class as
Die.java.
Write an application that randomly "throws" two dice and displays their values. After you read the chapter Making Decisions, you will be able to have the game determine the higher die. For now, just observe how the values change as you execute the program multiple times. Save the application as TwoDice.java.
View Replies
View Related
Apr 3, 2015
I'm trying to build a monopoly like game, and atm I'm trying to find way how to build the Community and Chance chest cards. so far, my logic is
1-create an ArrayList of cards with a given order
2-for a given number of times(for loop) generate 2 random numbers ,which will be the parameters for Collection.swap().
3-swap.
here's the code for the shuffler Button
shuffler.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i=0;i<shuffeledMessages.length;i++){
int randoma=(int)(Math.random()*4);
int randomb=(int)(Math.random()*4);
Collections.swap(myMessages,randoma,randomb);
}
}
});
For now things seem to work pretty ok, but I'm wondering if this is a good and efficient way to shuffle a card chest especially in case of large number of cards. plus, I'm not sure what would be a good loop count for effective shuffling, in my case I used i<arraylist.size
View Replies
View Related
Apr 4, 2015
How would I randomly assign colors and a with a name? Some functionality may require command-line arguments but I am a bit confused on how to structure this. Also, these names must appear on a color coded on a gui
View Replies
View Related
Dec 21, 2014
I'm trying to generate a random word from the array that I have made including the words by making it corresponding with a randomly generated number. So for example, if the number generated is 0, then the word that the person has to guess would be "AUNT". How would I transfer the randomly generated number from one method into the array method to get the word the person has to guess?
Write a program called Word Guessing Game. Open the file FourLetterWords.txt and write the contents into an array of Strings (the file has 87 words in it). Then use a randomly generated number between 0 and 86 to select a word. The player will then try to guess the word selected by the game. The player is allowed 7 tries, if the player does not guess the word on the 7th try he/she losses. Display the letter of the word as they are guessed in the correct order, you will also display the incorrect letters. The game is over when:
- The player completes the word, or guesses the whole word correctly.
- The player does not guess the word in seven tries.
The player must also be allowed to terminate the game.
The game must have at least 5 classes:
- Main Class
- Class to return a random integer between 0 and 86.
- Class to return a populated array of 87, 4 letter words.
- Class to return a character that the player enters from the keyboard.
- Class to display both the correctly guessed letters and the incorrect letters.
My code (it is not complete, my attempt to do what I am trying to do is obviously not working.)
import java.util.Scanner;
public class WordGuessingGame {
public static void main(String[] args) {
final int NUMBER_OF_WORDS = 87;
RandomWordGenerator.random(NUMBER_OF_WORDS);
[code]...
View Replies
View Related
Dec 14, 2014
For some reason, when I test out my code, my randomly generated numbers don't appear. Here is a sample result:
> What do you want to generate, integer, double, or character?
>integer
>What is the upper limit and lower limit of the integers you want to generate?
>1
>10
>How many integers do you want to generate?
>10
>BUILD SUCCESSFUL (total time: 9 seconds)
Is this because my code is not passing my parameters correctly? I'm not sure how to fix this either.
Here is my code for reference (it's not completed at the moment)
import java.util.Scanner;
public class NewNumberCharacter {
/** Main method
* @param args */
public static void main(String[] args) {
int return_int;
double return_double;
[Code] ....
View Replies
View Related
Apr 13, 2014
package guess.the.numbers;
import java.awt.*;
import javax.swing.*;
import java.util.Random;
import java.awt.event.*;
ublic class GuessTheNumbers extends JFrame{
private JButton guessBtn;
private JButton restartBtn;
[Code] ....
I am getting a strange error and it almost seems like its not comparing it to the random generated number just the guess that i entered before. Here are my error messages.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at guess.the.numbers.GuessTheNumbers$ButtonHandler.actionPerformed(GuessTheNumbers.java:119)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
[Code] .....
View Replies
View Related
Apr 16, 2014
Started learning about Array's I'm doing an exercise where you create a for loop that randomly assigns values to each element within the array, but where is my code going wrong?
import java.util.Scanner;
public class ArrayExamples{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double exampleArray[] = new double[5];
System.out.print("Enter a Number: ");
int num1 = input.nextInt();
[Code] .....
View Replies
View Related
Apr 5, 2014
I have an assignment for college that involves placing randomly chosen words from a text file into a 2-d array. I have nearly completed ithowever I am having difficulty with a list of string type.What I have done so far is below,
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.io.*;
import java.lang.Math;
[code]....
when I try to run an instance of the program(using BlueJ) I get an error saying there is a null pointer exception at line 35 which is wordsForGrid. add (puzzleWords.get(pos));
I am thinking that this is related to the fact that at this line in the loadWordsFromFile method List <String>words=new ArrayList<String>(); I could not create an empty list of string type, i.e List<String>words = new List<String>();
Is this correct? Or am I looking in the wrong place. The textfile I am using contains over 6000 words on a separate line for each if that makes any difference.
View Replies
View Related
Apr 14, 2015
I have this program where I'm supposed to fill an array with 1000 indices with 1000 randomly generated numbers between 1 and 1000. The program is supposed to check if any of the numbers match an index that is the same value (so for example, the number 4 is in index 4). How to check for that condition, especially using a binary search (I'm also told to use a binary search).
Right now the variable index isn't initialized because I don't know what to initialize it to exactly. How do I check to see if any numbers match the value of the same index?
import java.util.*;
public class Identity {
public static void main(String[] args) {
int [] integers = new int [1000];
// Fill array with randomly generated numbers
int [] display = GenerateRandom(integers);
[Code] ....
View Replies
View Related