Random Number Generator To Pick Index From Array
Oct 31, 2014
I am creating my 2nd project ever and its a hangman game. I created an Array that has 20 different words in it and need to create a function that generates a random number to pick a random index of the array to obviously pick a random word.
View Replies
ADVERTISEMENT
Oct 31, 2014
Im creating my 2nd project ever and its a hangman game. I created an Array that has 20 different words in it and need to create a function that generates a random number to pick a random index of the array to obviously pick a random word. I cannot figure out the syntax for the life of me.
View Replies
View Related
Apr 4, 2015
How do I compare an array (or maybe int?) (example: 3572) to a 4digit number from random generator? how many of the four digit match the generated numbers.
View Replies
View Related
May 18, 2015
I am working on a little nothing project, but I wanted to create a random number generator for a silly game where the user guesses the number.I have used google, but they are using LOG statements, what it does.
View Replies
View Related
Aug 29, 2014
I am brand new to programming and java. I decided I wanted to learn programming so now I'm in a class. I am into my second week and my assignment is to generate 100, 3 digit numbers then add them together. I understand how vast java is, but as of right now we have only studied if/else/switch statements, I was able to get the 100, 3 digit numbers generated, but I cannot add then together.
import java.util.Random;
import java.util.Math;
public class ThreeDigitGenerator {
public static void main(String[] args) {
int sum = 0;
[Code] ....
View Replies
View Related
Dec 1, 2014
I am new at coding and I can't seem to figure out why my random number generator (numGen) won't let me compile. this is the game class to a game i'm making called Bagels. its very similar to the game mastermind in the sense that you have to try to guess a three digit number and the computer will return feedback giving you clues that will guess the number.
import java.util.*;
import java.io.*;
public class BaglesGame {
public static numGen() {
Random Rainbow = new Random ();
firstDigit = "" + Rainbow.nexInt(10);
[Code] .....
View Replies
View Related
May 13, 2011
I am trying to make a phone number generator. I get the following error though when trying to compile.
my post
C:JAVA_PROGRAMMING_CODEcode1RandomPhoneNum>javac RandomPhoneNum.java
RandomPhoneNum.java:23: cannot find symbol
symbol : method toOctalString(int)
location: class RandomPhoneNum
strippedNum = toOctalString(num1);
^
1 error
Here is the code so far
import java.util.Random;
public class RandomPhoneNum
{
public static void main(String[] args)
{
System.out.println("This app prints out a random phone number.");
[Code] ....
Also, how did you people know about octal base numbers? I would never have known that. What I can do to improve my math ability?
View Replies
View Related
Jul 31, 2014
Basically this code is supposed to create an int array of 50 elements, then pull the elements listed in positions 1-6 to make a lottery draw.
Problem is it gets down to that last "For" statement and then says "duplicate local variable i." If I attempt to separate it from the previous for loop with curly braces, then it gets mad and says it doesn't know what "local variable i" even IS. So I tried changing it to "j" in the last statement and it couldn't recognise that either. I feel like I need to change the variable name in that second for loop but I'm not sure how to make it understand that the second variable is going to be outputting the values from the first variable.
public class Lottery {
public static void main(String[] args) {
// TODO Auto-generated method stub
int []nums = new int [50];
for (int i = 1; i <50; i ++) {nums[i] = i;}
[Code] ....
View Replies
View Related
Apr 24, 2015
I am trying to write a Random Number Generator that will not contain any repeat value in a given range
something this I have try so far
import java.util.Random;
/**
* This class is used to generate a
* Range of Random Number in a range from int a to int b
*/
public class RandomNumberGenerator {
public static void main(String[] args) {
int[] arr=randomNummbers(1, 20);
for(int j=0;j<arr.length;j++){
[Code]...
although this code generate random numbers but some values are also getting duplicate.So how to write a program for random number that will not repeat any integer in range?
View Replies
View Related
Nov 18, 2014
I've run into some problems with my Quiz-game program. What I basicly want it to do is generate a random number that is tied to the other windows I have for the program (I want a new random question to be generated once I have guessed on the first one). I've read about a few different ways to approach this but everything I get is a pure number generator.
I imported
import java.util.Random;
into my program but I don't know where to go from there.
View Replies
View Related
Apr 11, 2011
I've been working on a personal project again in Unit 3 of my text book and this time they want me to make a random phone number generator. Here's the actual directions:
"Write an application that creates and prints a random phone number of the form XXX-XXX-XXXX.
Include the dashes in the output.
Do not let the first three digits contain an 8 or 9 (but don't be more restrictive than that), and make sure that the second set of three digits is not greater than 742.
Think through the easiest way to construct the phone number. Each digit does not have to be determined separately."
With this assignment I came across 2 problems:
1. What I tried was that since the first 3 numbers have to be a random number with no 8's or 9's I tried doing this algorithm:
int first0, first1, first2, second, third, random0;
first0 = generator.nextInt (999);
random0 = generator.nextInt (8);
first1 = first0.replace('8', random0);
first2 = first1.replace('9' random0);
The problem is that Java won't allow me to generate random numbers this way. Are there any easier ways of generating numbers that doesn't contain specific number (such as 8 or 9 in this case?)
2. I was thinking about this throughout the whole project and what if I generate a number such as 27 (less than 3 or 4 digit number) rather than 3 or 4 digit numbers? How would it show 0027 or 027 instead of 27 for the phone numbers?
import java.util.Random;
public class pp0303 {
public static void main (String[] args){
Random generator = new Random();
int first0, first1, first2, second, third, random0;
[Code] ....
RESULTS:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Cannot invoke replace(char, int) on the primitive type int
Cannot invoke replace(char) on the primitive type int
Syntax error on token "random0", delete this token
at pp0303.main(pp0303.java:15)
View Replies
View Related
Nov 7, 2014
I`m trying to make a gradebook that uses random number generator to give the score, I also need to implement student numbers and sort everything with a total at the bottom. But I cant figure out how the bubblesorting works. This is my code so far:
package karakterbok;
import static java.lang.Math;
import java.util.Random;
public class Karakterbok {
public static void main(String[] args) {
Random karakterer = new Random();
Random studid = new Random ();
[Code]...
how to sort this using bubblesort?
View Replies
View Related
Aug 18, 2014
So I'm trying to make an applet that displays images of cards. The applet has a deck of 52 playing card images. It should shuffle the deck with a random number generator and display the first 10 cards of the shuffled deck.
With this code I have about 100 errors and I'm not sure what I'm doing wrong: I know I have to use arrays in order to ultimately display the images, but do I have to create Image objects for the Image [] cards array? I am getting many "class, interface, enum expected" errors as well as others.
import java.util.Random;
import java.awt.Image;
import java.applet.Applet;
import java.awt.Graphics;
public class unit12 extends Applet
{
[Code] .....
View Replies
View Related
May 9, 2015
Im trying to make a tic tac toe game that you play against the computer using a random number generator and two dimensional arrays for the game board. Im not trying to make a GUI, the assignment is to have the board in the console, which I have done. I have run into a few problems with trying to get the computer player to correctly generate 2 integers and have those two integers be a place on the game board. Here is my code so far.
import java.util.Random;
import java.util.Scanner;
public class TicTacToe {
private static Scanner keyboard = new Scanner(System.in);
private static char[][] board = new char[3][3];
public static int row, col;
[Code] ....
View Replies
View Related
Jan 29, 2014
int[] a = new int[7];
a[0] = 0;
a[1] = 10;
a[2] = 256;
a[3] = 57;
a[4] = 33;
a[5] = -154;
a[6] = 168;
[code]....
What program needs to find is the most biggest number. It does the job, but another task of the program is to find the index of that number . The second loop should do just that, but for some reason, as the loop goes further, it passes through the if statement even though answer "a[i]" is not equal to "answer". The idea is that if a[i] and answer are equal, the "i" should represent the index number.
View Replies
View Related
Apr 16, 2014
I'm making a program with several functions for an array of integers, one being to find the last occurrence of a given integer and returning it. The problem is that the method is returning -1 for every input, even though it's only supposed to do so for numbers not in the array. Here's the part of my code with the sections I'm having an issue with:
Driver
import java.util.Arrays;
public class ArrayMethodsDriver
{
public static void main(String[] args) {
int[] a = {7,8,8,3,4,9,8,7};
System.out.println("Last index of 8: " + ArrayMethods.findLast(a, 8));
System.out.println("Last index of 2: " + ArrayMethods.findLast(a, 2));
[Code] ....
As you can see, I'm testing 8(which should give a 6) and 2(which should give -1 because it's not in the array), and 4(which should give 4). The print statements all say -1.
View Replies
View Related
Jan 16, 2014
I'm studying about arrays and I have some questions:
First, is there any difference between these two?
Java Code:
int x[] = new int[3];
int[] x = new int[3]; mh_sh_highlight_all('java');
It seems to me when I try them they do exactly the same, is that correct?
Second, more important question. If I want to make an int variable that refers to the index number of an array, how do I write? For example if we have
Java Code: String[] string = new String[10]; mh_sh_highlight_all('java');
And I want to have a variable "int n" that refers to an index number, so that if I set n = 5 then string[5] is selected. Note that the int n is NOT an array, but just a regular integer variable. How can I do that?
View Replies
View Related
Sep 21, 2014
So im making this ghost game where i display an 8x8 filled with 0s and a randomly generator five 1s in there I can get it to display 0s and add 1s, however sometimes the 1s that are randomly generated sometimes go on the same spot making it look like there are only four 1s. How would i go about fixing that?
package Grade12;
import java.util.Random;
public class Ghost {
public static void main(String[] args) {
Random generator = new Random();
int gameboard [][] = new int [8][8];
int randomx, randomy, counter = 0, sum = 0;
for(int row = 0; row < 8; row++){
for(int col = 0; col < 8; col++){
(gameboard[row][col]) = 0;
}
[code]...
View Replies
View Related
Mar 16, 2015
I've been trying to get a card generator working, but hells bells nothing is easy in java (unless you already the correct answer) trying to figure out the correct way to apply a method you don't understand is like being asked for the German word for banana in Spanish class.
import java.util.Random;
import java.util.Scanner;
class jacktest
{
public static void main ( String[] args ) {
Random r = new Random();
[code]....
my next step might be using object (hammer >=computer)=watch telly instead.
View Replies
View Related
Nov 30, 2014
I am splitting a zip file and want to name each fragment with a random name. I want to use java coding for generating this random file name.
View Replies
View Related
Sep 25, 2014
In my account driver I am trying to get the user inputted account number to get the account by account number. In my code
System.out.println("Which Account number: "); int account = scan.nextInt();
ac.get(account-1);
This works if my accounts are numbered incrementally starting with one, I want it to match the inputted account number
System.out.println("Account number: "); int num = scan.nextInt();
I am thinking a for loop is probably needed. Here is my code:
public class AccountDriver {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<Account> ac = new ArrayList<>();
boolean more=true;
boolean again=false;
[code]....
View Replies
View Related
May 8, 2014
Keeping the passwords at 7 charactersHiding the "[object HTMLInputElement]" next to the passwordsMaking the "Clear" button work to clear the forum
<!DOCTYPE html>
<!-- passwordgenerator.html -->
<html lang="en">
[Code]....
View Replies
View Related
May 26, 2014
I'm trying to create a program that will be able to generate random Bingo cards. This is what I have so far.
Java Code:
import java.util.Random;
public class BingoCard
{
private int[][] card;
public BingoCard(Random random)
{
card = new int[5][5];
for (int c = 0; c < 5; c++)
[code]...
The problem with this program is that even with the checkForDuplicateValues method, which checks for duplicate values of a column and replaces the values, it still prints out bingo cards with duplicate values!
View Replies
View Related
Sep 11, 2014
The program is a Random String Generator; it takes user input (type of string, and length), and then generates a string with their requested method. Its extremly simplistic, but had some interesting coding challenges considering I only just did 'Hello World' a few days back xP I actually already have quite a few additions and modifications I want to make to it, but wanted to get a bit of feedback on it otherwise.
The Main Class, RandomStringGenerator.java:URL...
InputHandler.java:URL....
RandomGenerator.java: URL....
View Replies
View Related
Jul 10, 2014
I'm trying to make the basis of a complex (pseudo)random password generator, but instead of 4 chars it is printing a 3 digit number.
public class PasswordGen {
public static void main (String[] args) {
char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
int w = (int) ((Math.random() * 25 +1)); // pick a letter of the alphabet
[Code] ....
View Replies
View Related
Apr 1, 2014
Trying to generate random integers, based off user-input for amount of integers, and then sort them into an array. The problem is that the second method needs to be int[] but I cannot figure out what to make the return result. The instructions say it needs to be an int[] in the UML diagram, so I know it's not supposed to be void.
Java Code:
public void generateNewSecret() {
Random rand = new Random();{
for (int i=0; i<numDigitsSet; i++) {
secretNumber[i]= rand.nextInt(10);
System.out.println("" + secretNumber[i]);
[Code] .....
View Replies
View Related