random numbers

random numbers

Hello i need this code to be modified for me to display the random numbers, but not twice or more. I mean i need a number to be display once. This code allows some numbers to be displayed more than once.

Hi Friend,

Try the following code:

import java.util.*;
class RandomNumberExample {
static Random generator = new Random();
public static int get (int[] array) {
int rnd = generator.nextInt(array.length);
return array[rnd];
}
public static void main(String[]args){
int arr[]=new int[10];
Scanner input=new Scanner(System.in);
System.out.println("Enter 10 numbers: ");
for(int i=0;i<10;i++){
arr[i]=input.nextInt();
}
System.out.println();
System.out.println("Random numbers are: ");
int random[]=new int[5];
for(int i=0;i<random.length;i++){
random[i] = RandomNumberExample.get(arr);
System.out.println(random[i]);
}
}
}

Thanks
View Answers

June 30, 2010 at 4:17 PM

Hi Friend,

Try the following code:

import java.util.*;

public class RandomNumberExample {
public static List<Integer> get(List<Integer> numbers, int noOfRandomNumbers) {
List<Integer> list = new ArrayList<Integer>(numbers);
List<Integer> randomList = new ArrayList<Integer>();

Random rd= new Random();
for (int i=0; i < noOfRandomNumbers; i++) {
int index = (int) (rd.nextDouble()*list.size());
randomList.add(list.get(index));
list.remove(index);
}
return randomList;
}
public static void main(String[] args) {
int arr[]=new int[10];
Integer in[] = new Integer[10];
Scanner input=new Scanner(System.in);
System.out.println("Enter 10 numbers: ");
for(int i=0;i<arr.length;i++){
arr[i]=input.nextInt();
in[i]=new Integer(arr[i]);
}
List<Integer> randomList = get(Arrays.asList(in),5);
for (Integer randomNumber : randomList) {
System.out.print(randomNumber + " ");
}
}
}

Thanks









Related Tutorials/Questions & Answers:
random numbers
random numbers  hi.. i am creating a website and on feedback form to authenticate user i want to generate a random number on label.if user types that number correctly in textbox then he is allowed to submit feedback otherwise
random numbers
random numbers  Hi I need some help with the code for the this program. This program should randomly choose a number between 1 and 1000, and then challenge the user to guess what it is, giving hints ('too high!' or 'too low
Advertisements
Generate Random Numbers
Generate Random Numbers  hi,,, Please give me answer with example How do you generate random numbers within a given limit with actionscript... This function generate random numbers with in given limit
random numbers - Java Beginners
random numbers  write a program to accept 50 numbers and display 5 numbers randomly  Hi Friend, Try the following code: import...(); System.out.println("Random numbers are: "); int random[]=new int[5]; for(int i=0;i
Generating random numbers in a range with Java
Generating random numbers in a range with Java  Generating random numbers in a range with Java
random numbers - Java Beginners
random numbers  Hello i need this code to be modified for me to display the random numbers, but not twice or more. I mean i need a number...("Random numbers are: "); int random[]=new int[5]; for(int i=0;i  Hi
Random numbers - Development process
Random numbers  hi, How to generate unique random numbers between range like(10 to 50)  Hi friend, class RandomNumber { public static void main(String[] args) { //set the variable aNumber
How to generate a list of random numbers?
How to generate a list of random numbers?  Hi, How to generate a list of random numbers? I want code in scala programming language. Thanks  ... of Scala programming and it generates random integer. Following program
How to generate a list of random numbers?
How to generate a list of random numbers?  Hi, How to generate a list of random numbers? I want code in scala programming language. Thanks  ... of Scala programming and it generates random integer. Following program
How to generate a list of random numbers?
How to generate a list of random numbers?  Hi, How to generate a list of random numbers? I want code in scala programming language. Thanks  ... of Scala programming and it generates random integer. Following program
generate random numbers and display the largest
generate random numbers and display the largest  Hi, I am using... creates an array and fills it with random ints, prints the contents... ArrayRandom{ public static void main(String args[]){ Random r = new Random
Generate random numbers from 1 to 100
Generate random numbers from 1 to 100  1)A class Called: RandomNumberGenerator that generate random numbers from 1 to 100 2)A class Test that tests... an object from the Random NumberGenerator class to generate your input test data
generating random numbers - Java Beginners
generating random numbers  We would like to be able to predict tomorrow's price of a share of stock. We have data on past daily prices. Based on that we will make a prediction. Our plan is to use a weighted average of the 5 most
Generate random number between two numbers in Scala
Generate random number between two numbers in Scala  Hi, How to Generate random number between two numbers in Scala? Thanks   Hi, Following code can be used for generating random number in scala: val rand = new
Generate random number between two numbers in Scala
Generate random number between two numbers in Scala  Hi, How to Generate random number between two numbers in Scala? Thanks   Hi, Following code can be used for generating random number in scala: val rand = new
Generate array of random numbers without repetition
Generate array of random numbers without repetition In the previous section, you have learnt how to generate an array of random numbers from the given array... created an application that will generate the array of random numbers without
Generate array of random numbers
Generate array of random numbers You can generate a random number either...() but it is better to use Random class. If you want to generate a series of random numbers...(); } System.out.println(); System.out.println("Random numbers are: "); int random
Generate random numbers in Java
Generate random numbers in Java - How to use the java.util.Random class... or a set of random numbers in your program. We are using the java.util.Random... in Java which is used by developers to generate pseudo-random numbers in the Java
Generating Random Numbers to Fill array. Java Beginner needing help!
Generating Random Numbers to Fill array. Java Beginner needing help!  ... a program that produces random permutations of the numbers 1 to 10. eg... the permuted numbers. Make a second array and fill it with the numbers 1 to 10
Random in jsp
Random in jsp          Random numbers are the numbers that are determined entirely by chance. User does not have any control over the working of random numbers
Generating Random Number
;  In many places you need random numbers to fulfill your requirements. Java provides classes to help you in generation random numbers for your application. Random Number is the frequently used to generate numbers
How to generate random number in java
type numbers. Random Numbers in Java with Example As we discussed that to create... five random numbers. If you are using java Math.random  it will generate...How to generate random number in java In this section you will learn how
Random classes
Random classes  Hello... What is Random class? What's the purpose
Random classes
Random classes  Hello... What is Random class? What's the purpose
Random classes
Random classes  Hello... What is Random class? What's the purpose
getting random number in java
getting random number in java  getting random number in java Hi everyone, i am trying to get a random number in a particular range. for example random numbers between 1 to 100 in Java. Though i m new to Java, i have tried many
RANDOM ACCESS FILE CONCEPT
RANDOM ACCESS FILE CONCEPT  Write a program to use a File object and print even numbers from two to ten. Then using RandomAccessFile write numbers from 1 to 5. Then using seek () print only the last 3 digits
How do I generate random number?
there is a requirement of generating random numbers between two given numbers. How do I write..., The java.util.Random class can be used to generate the random numbers. Check the tutorial: Generate random numbers in Java. Thanks
random number
random number  Sir could u please send me the code registration form of roseindia.net with image varification
random number
random number   Please How do I generate a program that gives me random integer from 115 to 250? Using java.util.random. Thank you very much!  ... main(String[] args){ int min = 115; int max = 250; Random random
automorphic numbers
automorphic numbers  how to find automorphic number in java   Hi Friend, Pleas visit the following link:ADS_TO_REPLACE_1 Automorphic numbers Thanks
Pick at Random
the entry from the array before selecting another name at random
Random Access File.
Random Access File.  Explain Random Access File
sorting numbers
sorting numbers  How to sort the numbers in ascending order   import java.util.*; class SortNumbers{ public static void main(String...=input.nextInt(); list.add(num); } System.out.println("Numbers
numbers
Prime Numbers
Prime Numbers  Create a complete Java program that allows the user to enter a positive integer n, and which then creates and populates an int array with the first n prime numbers. Your program should then display the contents
Javascript random number - Design concepts & design patterns
are retreived from database.I have to generate random numbers corresponding to every... one group there will be 10 rows and their random numbers will be same.Then next 10 rows' random numbers will be same.coloumn name is LOT NO.After displaying
Numbers pyramid
Numbers pyramid  Hi sir, Can you please tell me how to output this using nested for loops? 1 2, 1 1, 2, 3 4, 3, 2, 1 1, 2, 3, 4, 5 6, 5, 4, 3, 2, 1 1, 2, 3, 4, 5, 6, 7 8, 7, 6, 5, 4, 3, 2, 1 1, 2, 3, 4, 5, 6, 7, 8, 9 10, 9, 8
Rational Numbers
Rational Numbers   Write and fully test a class that represents rational numbers. A rational number can be represented as the ratio of two integer values, a and b, where b is not zero. The class has attributes for the numerator
using random number
using random number  generate a 10 digit number using random number and display the length of longest increasing series
Listing all even numbers between two numbers
Listing all even numbers between two numbers  Hi, How to write code to list all the even numbers between two given numbers? Thanks   Hi, You function will take 2 numbers and the find the even numbers between
understanding buttons :JSP random no program
understanding buttons :JSP random no program  I hav java random... random no between 100 and 200 on loading and on clicking submit button it ives... "no is greater" please help me to understand buttons .thank you java code for random
Random Access File class
Random Access File class  Write a short note on Random Access File class.   Please visit the following link: Java Random Access File
PHP Random Number
PHP Generate Random Numbers: A random number is becoming more useful.... To generate random number, PHP provides rand() function. ... Value A random value  Example :ADS
how to add to numbers in java
how to add to numbers in java  how to add to numbers in java
Generate Random Integer Between Two Values in Scala
Generate Random Integer Between Two Values in Scala  Hi, I am working on the SCALA project and want to generate random integer between two numbers. How to Generate Random Integer Between Two Values in Scala? Thanks   
Generate Random Integer Between Two Values in Scala
Generate Random Integer Between Two Values in Scala  Hi, I am working on the SCALA project and want to generate random integer between two numbers. How to Generate Random Integer Between Two Values in Scala? Thanks   
Version of taglibs>random dependency
List of Version of taglibs>random dependency
Javascript random number - Design concepts & design patterns
are retreived from database.I have to generate random numbers corresponding to every... group there will be 10 rows and their random numbers will be same.Then next 10 rows' random numbers will be same.coloumn name is LOT NO.After displaying
Hexadecimal numbers multiplication
Hexadecimal numbers multiplication  Sir, I have to multiply 128 bit hexadecimal numbers. Do u have any logic for this?? The numbers are like ab7564fa342b5412c34d9e67ab341b58

Ads