Generating Random String Of Integers - No Repeating Numbers In Java

Nov 9, 2014

I don't want to use an array. I do not know of a function built into Java to allow me to do this.

View Replies


ADVERTISEMENT

Generating Random Numbers Without Repeating

Aug 21, 2014

I am working on a bingo project and created random numbers between 1 and 75 to generate. I have it set up that it shows the number in a text box and changes the color on the board if the number is called. When I test the program, some numbers are highlighted in addition to the number called. I believe this is because extra numbers are being chosen, but not being noted in the text box. Below is my code for the unique random numbers.

int CallNo;
String CallerTxt = new String();
public void CallNum() {
Random RandCall = new Random();
//Generate Caller Random Number
CallNo = RandCall.nextInt(75)+1;

[Code] ....

View Replies View Related

Generating Random Numbers Within Interval

Jan 13, 2014

I've been trying to find the easiest way to write to generate a number which is between intervals of a arbitrary min and a max value.I've been searching for this but I don't find this particular thing.I've found that this combination works:

Java Code: int guess = rand.nextInt(max - (min - 1)) + min; mh_sh_highlight_all('java');

But I wonder, is this really the easiest way of writing it?

View Replies View Related

Generate Random 100 Numbers From 0 To 9 In Array - Count How Many Integers There Are

Mar 7, 2014

I'm trying to generate random 100 numbers, from 0 to 9, in an array using Math.random, but it only outputs 0s which is very confusing to me. Also, I need to be able to count how many different integers there are like 0s, 1s, 2s... 8s, 9s.

Here's my code, I only got as far as the array then got stumped on the counting part.

import java.util.Arrays;
public class countDigits {
public static void main(String[] args) { 
//Create random generator and values
int numbers = (int)(Math.random() * 10);
int arrayCount = 1;

[Code] ....

and my results

0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0

View Replies View Related

Unable To Print Repeating Integers In Array

Aug 5, 2012

I have a problem where i have to write some code to read through an int array and print out the different integers and how many times they occur. For example:

if the array contained the numbers 1,4,2,3,5,4,4,7,5,4,3,6,8,6,4,

i would need the print to appear something like this

integer: 1, times: 1
integer: 4, times: 5
integer: 2, times: 1

etc

as the array is read sequentialy from element 0 to the end. the program should find the integer value in the first element (increment a counter by 1) and then search all other elements to see if the integer reoccurs (counter++ for each time it reappears). obviously the counter would be the 'times' value in the second column.

My problem is i can get this accomplished yet i cant get the program to recognise when it has already registered an integer in a previous element and skip to the next element looking for a new integer. SO for each element in the array it prints the integer and then searches the entire array for the integer again and increments the counter accordingly. so in essence there is a line printed for each integer the number of times it actually occurs.

I simply wish to have my code to find an integer and the number of times it occurs. and then disregard the elements it has registered the previous integer in.

Also, keep in mind that i am NOT permitted to use any new data structures/arrays for storing/remembering values. I can only use int and double variables throughout.

what i have so far is this: with the array being customerID[] :

Java Code:

int num;
int counter;
for(int count = 0; count < customerID.length; count++)
{
counter = 0;
num = customerID[count];
for(int count2 = 0; count2 < customerID.length; count2++)

[Code]...

The output for the int array 1,1,3,0,3,2,0,4,1,3, looks something like this:

Integer: 1, Times: 3
Integer: 1, Times: 3
Integer: 3, Times: 3
Integer: 0, Times: 2
Integer: 3, Times: 3
Integer: 2, Times: 1

[Code]...

and as you can see lines are repeated for each time the same integer occurs in a new element. i just need my code to skip an element if the integer has been previously encounterd and recorded. i.e i need it to look more like this :

Integer: 1, Times: 3
Integer: 3, Times: 3
Integer: 0, Times: 2
Integer: 2, Times: 1
Integer: 4, Times: 1

with the first encountered integer being checked throughout the array and then moving to the next integer/element which is not the same as any previous.

View Replies View Related

Find Sum Of Numbers In A Random String?

Jun 17, 2014

Writing a Java program to find sum of numbers in a random string.

(Input=abc235^%$q!12&3 output=250)

If a number has - sign then u should consider it as a negative number.(Input=abc-12t%^&$dcf22 Output=10)

View Replies View Related

Generating Compound Interest With Integers

Sep 15, 2011

I am trying to change this code to use only integers to calculate the compound interest.

// Compound-interest calculations with for.

public class Interest {
public static void main( String args[] ) {
double amount; // amount on deposit at end of each year
double principal = 1000.0; // initial amount before interest
double rate = 0.05; // interest rate

[Code] .....

And here is the output I get :

Why do I get the output after year 2? I assume it has something to do with the remainder.

I also have to format this output with the decimal point, etc.. which I think I will be ok with after I get through this part.

View Replies View Related

Generating Random Sentences

Nov 3, 2014

I have been set this task, which is supposed to make me code using string arrays. The idea is to generate random sentences.This is what i have been able to do so far :

package usingarraysagain;
public class sentences {
public static void main (String[] args){
String[] NOUNS = { "lizards",
"Nikola Tesla",

[code]....

View Replies View Related

Generating Random Number Between 1-1000

Oct 31, 2013

I am very new to programming. This is for a college assignment. It says in the brief of the assignment that we will need to convert Math.random to output a random number between 1-1000. How can I do this?

View Replies View Related

Generating Random Characters From ASCII Values

Dec 14, 2014

For part of my program, I am trying to ask the user for input on generating a number of random characters. I have already done this with integers and doubles, how would I do this for characters using ASCII values? Would I use the same format as I did for generating integers (Shown in code)?

import java.util.Random;
import java.util.Scanner;
public class NewNumberCharacter {
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);

[code]....

View Replies View Related

Generating Series Of Numbers - How To Get A Zero To Appear

Jun 8, 2014

My project is writing a program that generates a series of numbers to appear like a social security number (XXX-XX-XXXX). My code runs.. But any number below 10 it just shows one number (XXX-X-XXXX). What do I need to enter in to my code so that if the number is <10 it will show (00,01,02,03....)?

Java Code:

import java.util.Random;
public class IDnumber
{
public static void main (String[] args) {
Random generator = new Random()
int num1 = (generator.nextInt(7) + 1) * 100 + (generator.nextInt(8) * 10) + generator.nextInt(8);
int num2 = generator.nextInt(74);
int num3 = generator.nextInt(10000);
String IDnumber = num1 + "-" + num2 + "-" + num3;
System.out.println(IDnumber);
}
} mh_sh_highlight_all('java');

View Replies View Related

Permute Random Series Of Numbers Using Java Rand Function

Jan 26, 2014

So I have a program for a project that permutes a random series of numbers using Java's Rand function. Basically I use a seed and a number of items I want in the arrangement of numbers, and the program makes a permutation with no two numbers being repeated. Only arrays can be applied to it, so I've been hard at work finding a solution.

Here's the code so far:

public static int permutation[];
public static void permute(int numOfItems, int seed){
permutation=new int[numItems];
Random rand = new Random(seed);
permutation[0]=rand.nextInt(numItems-1);

[Code] ......

So basically I'm wanting the program to make a randomized list of numbers from the number of items I pass to the Permute method with no duplicates. I'm having some level of success with what I have written, as it gives me a randomized list when printing the output, but for some reason the first for statement in code never terminates fully, but instead runs indefinitely when generating the last integer.

For example, if I want to put 10 with a seed of 0 into it and make a list from 0-9, it will print 74283510, which is only 8 different integers. permutation[0] is manually set at the beginning of the method, which accounts for one more, but that's still only a list of 9, so I'm just wondering why the last integer is not being generated and why the program keeps looping and not terminating? I'm know for sure it's something I'm overlooking.

View Replies View Related

Print 2 Lists - 20 Random Numbers And Another List Without Duplicate Numbers

Feb 1, 2015

I'm trying to make a program that generates 20 random integers between 1 and 20 and then prints the list of random numbers to the screen. After that, I want to print a different list to screen with the same numbers from the first list only skipping any number that has been already printed to the screen. So two lists are printed to the screen. The first one has 20 random numbers. The second one has those same 20 numbers but only prints the numbers in the first list that aren't duplicated. So if m

y list of 20 random integers contains three 2s and two 14s, only one 14 and one 2 is printed to the second list. Currently, my code generates 20 numbers from 1 to 20 and stores those numbers in an array but I don't know how to print solve the second part of my problem. I don't know how to print the s different list only without duplicate numbers. As a result, my output is nothing because it doesn't print any number from the first list as oppose to skipping only duplicate one.

public void randomNum(){
System.out.println("Twenty random integers: ");
int max = 20; // max value for range
int min = 1; // min value for range
Random rand = new Random();
int[] all = new int[20];

[Code] ....

View Replies View Related

Sorting Array Of Random Integers

Dec 8, 2014

What the fastest way to sort an array of random integers. My instructor told me to look into an algorithm that uses 2n, but that is all he gave me. I am unable to find anything of the sort (pun intended there) on google, in my text, in my other java books or on here. The only thing that I have come up with are things like mergesort with the exception that the indicies of the Array have to be even. The program that I'm writing takes user input for a minimum to maximum range, the amount of numbers to fill the array in that range, and verbose.

I've tried bubbleSort but it takes forever to sort under the conditions below. I have seen a sorting algorithm that uses n (log n) but didn't really understand how that one worked.

Using the 2n algorithm, I should be able to test the range from 1 - 1000 and have it populate an array of of 1,000,000 random integers. It should be able to complete in a matter of miliseconds.

View Replies View Related

Generate Random Numbers Without Certain Numbers In Range

Jul 31, 2014

I tried out doing number (generated randomly) != (another number) but that does not work. If I for example want a number between 1 and 10, but I do not want the number 5, what can I do in order to make this happen?

View Replies View Related

Displaying Random Value Of Integers And Then Finding Missing Integer From Values In Array

Feb 7, 2015

I have a problem where I have to create random integers from 1 to n + 1. When the array is displayed I have to find the missing integer from the values that are displayed and show that value. I have to use O(n^2) and O(n) operations. I have created this code so far but I cannot figure out how to make my values display n + 1. It is only displaying values 1, 2, 3, 4 and 5 in random order. I also cannot figure out how to find the missing value. I have created a boolean displayed statement but can't determine how to use it in this code.

=Java
import java.util.Random;
public class Task6
{
public static void main(String[] args)
{
int[] numbers = new int[7]; //create array of numbers
Random random = new Random();
boolean displayed = false; //boolean value to determine if number was displayed

[code].... 

View Replies View Related

Swing/AWT/SWT :: Analog Clock Working But Seconds Repeating In Java

Oct 6, 2014

I made an Analog Clock and its working but when a remove the filloval (Background) the seconds hand keep repeating itself..here is the code

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.JFrame;
import javax.swing.JPanel;

[code]....

View Replies View Related

Sum Of Numbers Between Two Integers

Nov 19, 2014

I was trying to get sum of numbers between two given integer and managed to do it.

public int sum(int a, int b) {
int sum=0;
while(a <= b) {
sum+=a;
a++;
}
return sum;
}

in here; when I enter the values i.e 2 and 5, it calculates 2+3+4+5 and gives me the result 14.

my question is, how could I exclude a and b in this calculation ( in this case exclude 2 and 5 and return 3+4 )

Bonus question ; how could I only calculate the odd (or even) numbers between those integers?

View Replies View Related

How Can Two Same Integers Be Interpreted As Different Numbers

Sep 12, 2014

I have a code like following. x and y are both Integers and have same values (e.g. 5). But they are interpreted as different values. Then, it validates the following condition.

Java Code: if (x != y) {
"x and y are different..."
} mh_sh_highlight_all('java');

View Replies View Related

Making Integers Have Limited Numbers?

Aug 14, 2014

I want users to input their phone Number But I want to make sure it is a phone number so they don't just do 1 and then leave it.

public static void main(String[] args) {
// Create the Scanner
Scanner in = new Scanner(System.in);
// Create the Variables
int phone;
if(phone > 100000000000 || phone < 100000000000) {
System.err.println("Error");
}

By the Way there are 12 digits in the if statement so that is 11 0's. Because all phone numbers are 12 digits.But when I run it, it comes up with an error saying The literal 100000000000000000000 of type int is out of range.I don't know how to make it so that the int has a limited number.

View Replies View Related

Making Integers Have Limited Numbers

Aug 14, 2014

I want users to input their phone Number But I want to make sure it is a phone number so they don't just do 1 and then leave it.

public static void main(String[] args) {
// Create the Scanner
Scanner in = new Scanner(System.in);
// Create the Variables
int phone;
if(phone > 100000000000 || phone < 100000000000) {
System.err.println("Error");
}

By the Way there are 12 digits in the if statement so that is 11 0's. Because all phone numbers are 12 digits.But when I run it, it comes up with an error saying The literal 100000000000000000000 of type int is out of range.I don't know how to make it so that the int has a limited number.

View Replies View Related

Java Algorithm - Convert String Of Numbers Into Text

Feb 18, 2014

I need a Java algorithm that converts a string of numbers into text. It is related to how a phone keypad works where pressing 2 three times creates the letter "c" or pressing 4 one time creates the letter "g". For example a string of numbers "44335557075557777" should decode where 0 equates to a space.

View Replies View Related

Java Random Road Cross - Generate Random Number Between One To Ten

Dec 8, 2014

The program I'm supposed to create generates a random number between one to ten. Then the program is to ask me if I wish to cross the road. If you choose to cross, the outcomes for 0-2 are "You crossed safely." For 3-5, 75% of the time it should say "RIP you got run over", and 35% of the time it should say "You crossed the street." For 6-8, 60% of the time it should say you made it.", and 40% of the time it should say "You died". For 9-10, it should say "RIP".

So far I have gotten the random number generation part working. I have up to here:

import java.util.Random;
public class test4 {
public static void main(String[] args) {
Random random = new Random();
for(int i =0; i < 1; i++){
int num = random.nextInt(10) + 1;
System.out.println("The number of cars on the street are: " + num + "Do you wish to cross the road?");
}
}
}

View Replies View Related

Method To Take Array Of Integers And Rearrange Numbers From Least To Greatest Using For Loops

Apr 22, 2014

I'm making a method to take an array of integers and rearrange the numbers from least to greatest, using for loops.

I'm getting the error "java. lang. ArrayIndexOutOfBoundsException: 8,

Portion of the ArrayMethods class with the sorting method

Java Code:

public static Integer[] sortArray (Integer[] a)
{
int swap;
for (int i = 0; i < a.length; i++)

[code]....

View Replies View Related

Generate 10 Random Integers / Store In Array And Then Calling A Method To Display Array

Nov 8, 2014

So I need to generate 10 random integers in the range 1-20 but i have to store them in an array called numbers. Then I have to call a method called displayArray which displays the contents of the array and for the assignment i have to use a for loop to traverse the array. The method header for the displayArray method is:

public static void displayArray(int[] array)

This is what I have done
 
public class RandomIntegers {
 static int numbers = 0;
public static void displayArray(int[] array) {
System.out.println(numbers + "Numbers Generated");

[Code] .....

View Replies View Related

For Loop With Random Numbers?

Jul 5, 2014

Assume that vehicles are going through a two-way traffic intersection. There are three types of vehicles: car, motor bikes and trucks. Generate a series of 10 random integers, between 1 and 3,inclusive.The numbers represent the type of vehicle as stated below:

NumberVehicle Category
1Car
2Motor bikes
3Trucks

Write a program, using a for loop, to count how many vehicles going through the traffic intersection are cars, motor bikes and trucks. Then, the program should print out the numbers for each vehicle category. There is no user input for this program. How do i do it so they will add up the sum of each vehicle?

The answer should be something like

Number of cars = X
Number of motor bikes = Y
Number of Trucks = Z

but i'm getting

Total number of vehicle:

cars
motorbikes
motorbikes
cars
Trucks
Trucks
motorbikes
motorbikes
Trucks
cars
public static void main(String[] args) {

[code]....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved