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


ADVERTISEMENT

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

Checking Divisibility Of A Series Of Numbers?

Oct 27, 2014

write a java program to check divisibility of a series of numbers that a user inputs the starting and ending numbers and also the number to check divisibility with.

The problem I am having is it only checks the first number and doesn't check the rest.

import java.util.Scanner;
public class MyMidTermAssignment
{
public static void main(String[] args) {
int numberInputBeginning,numberInputEnding,divisibleByNumber;
System.out.println("Enter first number of series");

[code]....

View Replies View Related

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 View Related

For Loop - Calculate Total Of Series Of Numbers?

Nov 10, 2014

So basically, I've been trying to create a for loop that asks the user for a number between 1 and 30, then calculates the total of the series of numbers. For example, if 15 is entered, the calculation should be 1/15+2/14+3/13+...15/1 which would equal approximately 38.1. The problem I'm having is that the code doesn't loop whenever I type a number for some reason, which results in a very incorrect calculation. Here is my code so far:

import java.util.Scanner;
public class HmwLoop {
 public static void main(String[] args) {
double sum = 0;
for (double num1 = 1, num2 = 30; num1 <= 30 && num2 >= 1; num1++, num2--)

[Code] .....

View Replies View Related

Display Largest And Smallest Numbers In A Series Of 10 Inputs

Apr 15, 2014

I have to get the user to enter in 10 numbers and with those 10 numbers I have to find the total, average, smallest, largest numbers in the set the user inputs. I have the total and average already figured out but how would you go about trying to find the largest and smallest numbers within this set of code.

import java.util.*;
public class testhw7
{
public static void main (String [] args) {
Scannernumberin = new Scanner (System.in);
doublevalue[];
doubletotal;

[Code]...

View Replies View Related

Adding Fractions - Loop For Calculating Total Of Series Of Numbers

Mar 8, 2014

I'm just not noticing why it won't display the answer. I'm trying to solve this book problem......

"Write a for loop that calculates the total of the follower series of numbers:

1/30 + 2/29 + 3/28......+30/1"

Here is what I have..

public static void main(String[] args) {
double total = 0;
for (double a = 1, b = 30; b < 1; a++, b--) {
total += (a / b);
}
System.out.println(total);
}
}

When launched, the output is 0.0. I tried changing the variables a and b to doubles but didn't change anything...

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

Random Shuffle Array Of Card Objects Which Does Function Of Deck

Oct 26, 2014

I have to random shuffle an array of Card Objects which does the funcion of a deck. Heres the code:

Java Code:

public void barajear(){
int j;
for (int i=0;i<52;i++){
j=Baraja.random(51);
if (this.mazo[j]==null){
this.mazo[j]=this.arreglo[i];
}else{
--i;
}
}
} mh_sh_highlight_all('java');

so bassically theres an array called "arreglo" which has the cards in order and the function "random" its an rng of numbers from 0 to 51.what i'm trying to do it's to take the cards from the ordenated array and put them randomly in the other but only if it's empty.(the array "mazo" has alredy been initialized with null).it worked at first, but now, after compiling succesfully i tried to run it and the cmd just...

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

Flip Function - Making Coin Stop On Random Heads Or Tails

Nov 19, 2014

I cannot figure out why my buttons don't work. I believe my problem is in the flip function. I am very new to programming and I am about as good at code logic as I am at real logic lol. Also I am lost on how to go about making the coin stop on a random heads or tails.

Coin cF;
void setup() {
size (600, 600);
cF = new Coin(0,0,0);
}
void draw() {
background(100,100,100);
//Coin(int lX,int lY, int cSize)
//location(int l_x, int l_y)
//flip()

[Code] ....

View Replies View Related

Extending Calculator - Added Function To Square Numbers

Feb 14, 2014

I am new to java and during my classes we built a basic calculator. I want to extend my calculator and added function to square numbers.Here is my program:

Java Code:

package calculator02;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

[code]...

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

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

Create Random Set Of Numbers

Feb 4, 2015

I have a class project working on that is supposed to ask the user for input and create a random set of #'s in 4 rows. However it compiles but doenst run At one point Eclipse was telling me Scanner not closed?

import java.util.Scanner;
public class Matrixbuilder {
// input a number that is used to build a matrix. ie = 4 rows and 4 colums
//get the number
//build the matrix of random integers
//print the matrix
 
[code]...

View Replies View Related

How To Find A Sum Of Series In Java

Nov 1, 2014

how to find the sum of the following series

f(x)= 1+3+5+.......+2n+1

this what I did

Java Code: for ( n=0;x>0 && x<2*n+1;n++)
{
sum=sum+2*n+1;
n++;
System.out.println(sum); mh_sh_highlight_all('java');

Is it infinite loop?

is this correct ?

View Replies View Related

Do While Loop To Generate 6 Random Numbers?

Mar 2, 2014

I am currently taking a class in the field. My assignment is to generate 6 unique random numbers using a "Do While" expression. I might be mistaken but doesnt the inner loop execute first and then it works its way out? With this logic I believe my code should work but then again its not.

public class DoLottery {
public static void main (String args[]) {
int max= 10;
int random;
int random2;
int random3;

[code]...

I originally had output at the end but decided to comment it out to see if code would execute if I placed it every time the test was true.

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 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

Merge Sort With Random Numbers

Mar 2, 2015

How do i make this merge sort take on random numbers in an array instead of hard coding the numbers

Integer [] a = {8, 2, 6, 7, 5, 4, 3, 1};
mergeSort(a);
System.out.println(Arrays.toString(a));
}
public static void mergeSort (Comparable [] a){

[code]....

View Replies View Related

How To Shuffle Array Using Random Numbers

Nov 10, 2014

For a project we have to "shuffle" items in an array using random numbers. We are supposed to generate random numbers and use those numbers to exchange array elements. But I am not sure what that means, "exchange array elements". Does that mean you generate 2 random numbers within the length of the array, and then switch the items at those locations in the array?

View Replies View Related

User Must Get Three Random Numbers In A Row - Do While Loop

Feb 12, 2015

I need to fix a program in which the user must get a three random numbers in a row. They have five chances. This is what i have so far:

public static void main(String[] args)throws IOException {
// TODO code application logic here
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int num1=(int)(Math.random()*(9-0));
int num2=(int)(Math.random()*(9-0));
int num3=(int)(Math.random()*(9-0));

[Code] ....

View Replies View Related

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

Random Array - Only 2 Of 10 Possible Numbers Are Generated

Nov 21, 2014

I am writing a program that creates an array with random numbers. Then the user can choose what number of the array he/she wants to check the occurance of. This works fine, but the numbers generated seems very weird, only 2 of the 10 possible numbers are generated. 0 and one of the other 9 numbers, the 0 is always 10100 and the other one is always 101.

import java.util.Random;
import java.util.Scanner;
public class OppgaveC {
public static void sjekk() {
int randomArray[]=new int[101];
int countArray[]=new int[10];
Random rand = new Random();

[Code] ....

it also says i have a memory leak on my scanner, ow can i close this?

View Replies View Related

Fibonacci Series Using Java Multithreading

Feb 8, 2014

Program to generate Fibonacci series program using java multithreading.

View Replies View Related

Random 4 Char Generator Generates 3 Numbers Instead?

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







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