This question already has an answer here:
Select a random value from an Array
(1 answer)
Closed 8 years ago.
I'm writing a program that generates co-primes of a number.
Now for example a number 'A' has 50 co-primes, my objective is to randomly select a co-prime from the list of all co-primes generated for the number A.
Again for example:
consider a number 15, it has co-primes - {1, 2, 4, 7, 8, 10, 11, 13, 14}. So now i have to select randomly from these values. Likewise, if i generate an array of any values, then how to randomly select from this array.
So in general my question is how to generate a random number from the array of numbers that i have. Now, those numbers in the array can be anything. Like not necessarily natural numbers, or prime numbers, etc.
So is there any java function to do so. I've burnt my brain searching the internet, but didn't find one. I usually go for finding result on google, rather than asking quetions on forums. But when one gets exhausted, it's better to ask experts out there who might have faced similar problems.
Thanks in advanced!!
Is that what you want ?
int[] arr = { 1,5,9,3,2,7 };
Random rd = new Random();
int dice = arr[rd.nextInt(arr.length)];
You can use the java.util.Random class for this:
public int chooseRandom(int[] coPrimes) {
//Creates the Random instance
Random randomizer = new Random();
//Generate a random integer between 0 and the length of the array (exclusive)
int value = randomizer.nextInt(coPrimes.length);
//Return the element at that generated index
return coPrimes[value];
}
Related
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 3 years ago.
I am trying to generate a random number between a given range and 0.
The code given below helped me to generate a number between the given range.
(int)(Math.random() * 13 + 4);
Is it possible to modify this code to generate a value between 4 and 10 and also 0
use this for generate a value between 4 and 10
public static double getRandomDoubleBetweenRange(int 4, int 10){
double x = (Math.random()*((10-4)+1))+4;
return x;
}
I suspect that this is a homework question so I won't spoonfeed you with the correct answer but give you the tools you need to answer it yourself:
public static double random()
Returns a double value with a positive sign, greater than or equal to
0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.
Source: https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#random--
Casting a double to an int performs a narrowing primitive conversion. In the range you use and for positive numbers, you can just treat it like a floor (removing the numbers after the decimal point).
If you want to know about the details, see: https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3
Something like this would do the cause.
//stream 3 random numbers from 0 to 10 and pick any of them
int random = new Random().ints(3, 0, 11).findAny().getAsInt();
//print it
System.out.println(random);
UPDATE 2:
// make a list of 0 and 4-10
List<Integer> list = Arrays.asList(0,4,5,6,7,8,9,10);
// used for picking random number from within a list
Random random = new SecureRandom();
// get random index element from a list
int randomNumber = list.get(random.nextInt(list.size()));
// print
System.out.println(randomNumber);
This question already has answers here:
How do I declare and initialize an array in Java?
(31 answers)
Closed 8 years ago.
I need to create a list of available television channels (identified by integers). I am thinking that I would begin by creating int [] list = 5,9,12,19,64. Here is the code:
public NotVeryGoodTV(int[] channels) {
int [] list =5,9,12,19,64;
but I am getting a syntax error stating that a { is needed after "=". I would like to have a list of tv channels that would be available to the user once they turned on their television.
This is syntactically correct (instead of your array declaration):
int[] list = {5, 9, 12, 19, 64};
But it is not random, if you want to create an array with random integers:
Random randGen = new Random(); //random generator: import java.util.Random;
int maxChanNumber=64; //upper bound of channel numbers (inclusive)
int minChanNumber=1; //lower bound of channel numbers (inclusive)
int amountOfChans=5; //number of channels
int[] list = new int[amountOfChans]; //create an array of the right size
for (int k=0;k<amountOfChans;k++) //populate array
list[k]=minChanNumber+randGen.nextInt(maxChanNumber-minChanNumber+1);
Actually this codes does NOT check if you generate a different channel number (integer) for every item of the array: it is possible that in the array you will find two or more times the same number, but it is not difficult to adapt the code to avoid this, anyway the direction to take to have really random channel numbers is this one.
Replace:
int [] list =5,9,12,19,64;
With:
int[] list = { 5,9,12,19,64 };
The brackets tell Java that you are declaring a list.
However the numbers are not random; they are the ssame every time.
replace line 2:
int [] list =5,9,12,19,64;
with this code:
int[] list = {5,9,12,19,64};
Yep, you need to encase the list in curly braces like this:
int [] list = {5, 9, 12, 19, 64};
Just proper Java syntax issues.
This question already has answers here:
Random number,with nonuniform distributed [duplicate]
(5 answers)
Closed 8 years ago.
//Generates a random number but doesn't allow the same number be repeated
for (int i = 0; i < questions1.length; i++)
{
//random number
int r = (int)(Math.random() * i);
temp = index[r];
index[r] = index[i];
index[i] = temp;
}
How do I make it favour 1 number in particular?
If you want, for example, a random number between 1-6, but want 3 to be picked twice as likely as any other number, a very simple solution would be to create an array with 7 indexes. 1, 2, 4, 5, 6 all hold one index each. 3 holds two indices. Now pick a random number between 0 and 6 and return whatever number is in that index.
Using this approach you can provide whatever weights you want to any range of numbers.
There are almost certainly more elegant solutions, but this will get the job done.
You could build an List and fill it with the numbers you want in the proportion you want. Then use Collections.shuffle.
public void test() {
// Throw 1 twice as likely and 6 3 times as likely as the other numbers.
List dice = Arrays.asList(1,1,2,3,4,5,6,6,6);
Collections.shuffle(dice);
}
Let's say your target range is [0, 6] and you give 1 twice the likelihood. I'll give you two methods which won't eat your memory:
Select a random number out of [0, 7] and map 7 to 1
Select a random out of [0, 6] and if it's not 1 return a new random number out of [0, 6].
Suppose I wanted to generate random numbers taken from ArrayList:(1,2,3,4,5,6,7,8,9,10)
A Random Generator produces 5.
List gets updated- AL:(1,2,3,4,6,7,8,9,10)
Next Random Number cannot be 5.
I am writing a program that generates random numbers from a arraylist and once it generates the random number the list removes that number and the next random generated digit cannot be that number.
ArrayList<Integer> numsLeft = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8,9,10));
Random randomGenerator = new Random();
int number = 0;
String cont;
do
{
number = randomGenerator.nextInt(numsLeft.size());
numsLeft.remove(number);
System.out.println (number + " continue (y/n)");
cont = (stdin.readLine());
}
while (cont.equalsIgnoreCase("y"));
But the only thing I can do here is lower the size...
http://docs.oracle.com/javase/7/docs/api/java/util/Random.html
The easier approach is to simply shuffle your list then use the numbers in the shuffled order:
List<Integer> nums = new ArrayList<Integer>();
for (int i = 1; i < 11; i++)
nums.add(i);
Collections.shuffle(nums);
Now they are in random order, just use them one by one:
for (Integer i : nums) {
// use i
}
You could make an array of the available numbers. Then, the random number generator gives you the position in that array for the number that you want.
Probably a linked list or something would be more efficient, but the concept is the same.
So, with your example, you'd pull 5 the first time. The second time, you'd have this in your list:
1, 2, 3, 4, 6, 7, 8, 9
If your random number was 5 again, the fifth position is 6. Pop the six out, shift 7, 8, 9 over one, and decrement your random number generator to be 1-8 instead of 1-9. continue on.
of course, looking at your code, it looks like that is what you are trying to do already.
What seems to be the issue with your code? What results are you getting?
number = randomGenerator.nextInt(numsLeft.size());
numsLeft.remove(number);
You are now printing the random index that you are generating, not the number that was removed from the list. Is that what you wanted? I think you really meant this:
int index = randomGenerator.nextInt(numsLeft.size());
number = numsLeft.remove(index);
You could also do this using by randomly shuffling the list and then just going through it:
List<Integer> numsLeft = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5,6,7,8,9,10));
// Shuffle the list randomly
Collections.shuffle(numsLeft);
do {
// Remove the first number each time
int number = numsLeft.remove(0);
System.out.println (number + " continue (y/n)");
cont = (stdin.readLine());
} while (cont.equalsIgnoreCase("y"));
Why don't you create a hash map to take care of this. So your hash map can contain something like
Map[(1,1), (2,2), (3,3), ...] or Map[(1,true), (2,true), (3,true), ...]
So if you generate a number, then you can do something like:
String value = map.get(key); or boolean present = map.get(key);
if(value != null) or if(value == present)
map.remove(key), or you can even update the data and instead of removing the key you can update it and add the word removed or a boolean as previously suggested. But this way you can keep track of all the entries and removals in your map for each of the key values which would be your list of numbers.
remove can be pretty expensive operation when list is long. Shuffle is too - especially if you only need a few numbers. Here is another algorithm (it is famous but I can't find the source right now).
put your N (ordered) numbers in a list
Choose a random number m between 0 and N-1
Pick the element at location m. This is your unique random number
SWAP element m with the LAST element in the array
Decrement N by 1
Go to step 2
You "set aside" the numbers you have used in step 4 - but
Unlike shuffle, your initialization is fast
Unlike remove, your remove operation only takes moving one element (instead of, on average, N/2)
Unlike the "pick one and reject if you saw it before", your efficiency of picking a "new" number doesn't decrease as the number of elements picked increases.
I can create random numbers from a range using
Random rand = new Random();
int num = rand.nextInt(10);
System.out.println("Generated Random Number between 0 to 10 is : " + num);
But if i need the next random number generated to be a part of the range minus the already generated one, will keeping the above statement in a loop will suffice?
Also I need to stop once i exhaust all the number from the range.
For eg,
The code gives me a random number between [0-10],
1st - 4 - range {[0-10]}
2nd - 9 - range {[0-10]-4}
3rd - 8 - range {[0-10]-4,9}
..
..
10th - 10 - range {[0-10]-[0-9]}
Will this function output from [Range]{this is my requirement} or (Range)? Or is there any better alternative solution?
The easiest way would probably be to:
put the numbers (0 to 10) in a list
call Collections.shuffle() on that list
loop over the list
Something like:
List<Integer> numbers = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 10);
Collections.shuffle(numbers);
System.out.println(numbers);
You can even provide a random generator for the shuffling operation if the default doesn't suit you.
I do not think this is possible. Try to remember the numbers you already generatedin a list and use it to generate more random numbersuntil you find one that is not in your list.