Generating 5 unique coordinates on a grid using 2 arrays - random

I am using Java to program an application that randomly generates 8 X points in a 5x5 grid.
Both the x and y axis have separate arrays I use to store the coordinates. These 2 arrays are parallel arrays that correlate with each other. The problem I am having is I can not seem to generate 8 unique coordinates.
For example, no matter what I do, I will eventually only have less than 8 unique coordinates because it so happens that the same coordinates are generated twice.
I am trying to find an algorithm that can ensure the 8 random generated coordinates to be unique.
Also, For some reason, when I use the Random class to generate random numbers between 1 - 5, I sometimes get a 0 as a randomly generated number.
This is how I use the Random class:
Random rand = new Random(System.currentTimeMillis());
coordinate = rand.nextInt(5)+1;
Thank you all for your time!
code:
int[] co1 = new int[8];
int[] co2 = new int[8];
ArrayList<Integer> uniqueCo1 = new ArrayList<>();
ArrayList<Integer> uniqueCo2= new ArrayList<>();
for(int i = 0; i < 8 ; i++)
{
co1[i] = rand.nextInt((5)+1);
co2[i] = rand.nextInt((5)+1);
while(co1[i] == 0 || co2[i] == 0)
{
coord1[i] = rand.nextInt((5)+1);
coord2[i] = rand.nextInt((5)+1);
}
int j = 0;
for(int q = 0; q < acceptedCoord1.size() || j < acceptedCoord2.size(); q++)
{
while(co1[i] == uniqueCo1.get(q) && co2[i] == uniqueCo1.get(j))
{
co1[i] = rand.nextInt((5)+1);
co2[i] = rand.nextInt((5)+1);
}
j++;
}
uniqueCo1.add(co1[i]);
uniqueCo2.add(co2[i]);

Related

Generate unique random numbers in Dart

How can I generate two different random numbers in dart?
I have generated two random numbers using code below.
int rand1 = Random().nextInt(16);
int rand2 = Random().nextInt(16);
if(rand1 == rand2)
// generate new random
How do I generate new random numbers until rand1 != rand2?
If you need to have two different random numbers in a fixed range, then the simplest approach would be:
var random = Random();
var n1 = random.nextInt(16);
var n2 = random.nextInt(15);
if (n2 >= n1) n2 += 1;
This ensures that the first number can assume all 16 values, and the second number can assume any of the remaining 15 values, and with as even a distribution as the random generator allows.
For two ordered distinct numbers in the range 0..15, there are 16 * 15 possible outcomes, and you probably want each outcome to be equally probable.
This code achieves this by picking the first number at random, and then picking the second number to be one of the numbers different from the first by ensuring that it is in either the range [0 .. (n1 - 1)] or the range [(n1 + 1) .. 15] ... by picking a number in the range [0 .. 14] and adding one if >= n1, shifting the range [n1 .. 14] into [(n1 + 1) .. 15].
You can do this for more numbers, but you have to do more tests and additions.
I would suggest you a different approach, looping can be pain.
// create a list say of 16 numbers.
List list = List.generate(16, (i) => i);
// shuffle it
list.shuffle();
// take the numbers now, they are always unique
int firstRandonNum = list[0];
int secondRandonNum = list[1];
This is the simplest way to do it:
import 'dart:math';
void main() {
int rand1 = Random().nextInt(16);
int rand2 = Random().nextInt(16);
while(rand1 == rand2){
rand1 = Random().nextInt(16);
rand2 = Random().nextInt(16);
}
print('$rand1 $rand2');
}
To generate any 5 unique random numbers under a range 0-49
List<int> numberList = [];
Random randomizer = new Random();
while (numberList.length < 5) {
int random_number = randomizer.nextInt(50);
if (!numberList.contains(random_number)) {
numberList.add(random_number);
}
}
A potential solution for pulling many out of many (say, 50 out of thousands) is to loop and memoize with a hash set.
I pulled this from my code, but something like:
var hashSet = HashSet();
for (var i = 0; i < boardSize; i++) {
words.add([]);
for (var j = 0; j < boardSize; j++) {
Random _random = new Random();
String wordToAdd = abstractPossibleWords[_random.nextInt(abstractPossibleWords.length)];
while (hashSet.contains(wordToAdd)) {
wordToAdd = abstractPossibleWords[_random.nextInt(abstractPossibleWords.length)];
}
words[i].add(wordToAdd);
}
}
maybe useful for you.
'max' is exclusive. the unique result number is in the return List.
List<int> getRandomNums(int countOfNum, int max) {
int num = 0;
List<int> numList = [];
var random = Random();
int i = 0;
while ( i < countOfNum) {
int oldNum = num;
num = random.nextInt(max);
if (numList.contains(num)) continue;
numList.add(num);
i++;
}
return numList;
}

Longest straight in a matrix

I came across a programming challenge that has been tearing me up for those. It's named "dice straight".
Basically, you're given m x 6 matrices (the column length is always 6 but the rows vary), and you're to find the length of the longest string of adjacent integers that can be formed picking one integer from each row. That means, you can't take 2 integers from one row and you can't leave a row out.
For example, one of the matrix is
[4 8 15 16 23 42]
[8 6 7 5 30 9 ]
[1 2 3 4 55 6 ]
[2 10 18 36 54 86]
The longest straight can be formed taking 2 from row4, 3 from row3, 4 from row1 and 5 from row 2. That's 2,3,4,5. So it returns 4, the length.
The code I have now works for the above matrix and moderately sized matrices, but when it gets to 100 x 6 matrices, it runs forever. I'm using a combination of back track algorithm and depth first. First, I combine the rows into one array and sort them, then, using a stack, I pick numbers on the condition that it's adjacent to the stack top and it's not on the same row as any integer in the stack, and when it reaches a dead-end, I dequeue until I get to last working solution.
static int maxLength(Face[] faces, int d){
faces = quickSort(faces, 0, faces.length - 1);
for (int i = 0; i < faces.length - 1; i++) {
if(faces[i].side == faces[i+1].side && faces[i].dice > faces[i+1].dice){
Face temp = faces[i];
faces[i] = faces[i+1];
faces[i+1] = temp;
}
}
//System.out.println(faces.length);
for (int i = 0; i < faces.length; i++) {
//System.out.print(faces[i]+" ");
}
int maxLength = 1;
for (int i = 0; i < faces.length; i++) {
Stack<Face> tree = new Stack<>();
Stack<Integer> dices = new Stack<>();
Stack<Integer> sides = new Stack<>();
int length = 1;
tree.add(faces[i]);
dices.add(faces[i].dice);
sides.add(faces[i].side);
int pos = i;
//System.out.print(faces[pos].side + ", ");
while(!tree.isEmpty() && pos < faces.length - 1){
pos++;
//System.out.println(faces[pos] + "}"+faces[pos + 1]);
if(faces[pos].side - faces[pos - 1].side == 2){
for(Face ig : tree){
//System.out.print(ig+" ");
}
//System.out.println("/"+faces[pos]+"/"+length);
i = pos;
break;
}
faces[pos].visited = true;
//System.out.print(tree.peek().side + " ");
if(!dices.contains(faces[pos].dice) && faces[pos].side - tree.peek().side == 1){
tree.push(faces[pos]);
dices.push(faces[pos].dice);
//System.out.print(faces[pos].side + " ");
length++;
maxLength = Math.max(length, maxLength);
if(maxLength == d){
i = 5000000;
//System.out.println("beeeeeee");
//System.out.println(tree.size());
for(Face ig : tree){
//System.out.print(ig+" ");
}
break;
}
}else if(faces[pos].side - tree.peek().side == 2){
Face a = tree.peek();
Face x = tree.pop();
//System.out.println(tree.peek()+"|"+x+"|"+length+"|"+faces[pos]);
pos = Arrays.asList(faces).indexOf(x)+1;
dices.pop();
length--;
for(Face ig : tree){
//System.out.println(ig);
}
//System.out.println();
}else if(faces[pos].side - tree.peek().side > 2){
}
}
//System.out.println();
for(Face ig : tree){
//System.out.println(ig);
}
}
return maxLength;
}
"Face" is a class with 2 fields. 'Side', which represents the integer in a row i and dice, which represents 'i'. Face[] is an array of all the elements in the matrix, sorted in ascending order of 'side'. So Face[] for the above matrix would be
1(2) 2(2) 2(3) 3(2) 4(0) 4(2) 5(1) 6(1) 6(2) 7(1) 8(0) 8(1) 9(1) 10(3) 15(0) 16(0) 18(3) 23(0) 30(1) 36(3) 42(0) 54(3) 55(2) 86(3) in x(y) format where x is side and y is dice.
I found the challenge in the link below. If you don't understand my question, please check this.
https://code.google.com/codejam/contest/6314486/dashboard#s=p0&a=0
I'll very much appreciate it if anyone points me in the right direction. Thank you.

Create Random Number List With No Repetition

I'm looking to create a list of 'random' numbers from 1 to 15 but without any repetition. I have created an array and looking to store each number in it but can't figure out how to do this. I've gotten as far as creating the random list and storing them in the array but can't quite get to ensure there are no repetitions. Any help would be appreciated. My code is as follows:
int[] myList = new int[15];
Random random = new Random();
for (int i = 0; myList.Length; i++)
{
myList[i] = random.Next(1, 15);
}
Because the size of your list is equal to the possible values, you can just create the list in normal order:
int[] myList = new int[15];
for (int i = 0; i < myList.Length; i++)
{
myList[i] = i + 1;
}
and then shuffle it, for example by assigning a random value to each entry and sort by that value:
Random random = new Random();
myList = myList.OrderBy(a => random.Next()).ToArray();
You can do it using Fisher–Yates shuffle.
Sample Implementation:
int n = 15;
int[] myList = new int[n];
Random random = new Random();
for (int i = 0; i < n; i++)
{
myList[i] = i + 1;
}
for (int i = n - 1; i >= 1; i--)
{
int j = random.Next(1, i);
int temp=myList[i];
myList[i]=myList[j];
myList[j]=temp;
}
You need to get the algorithm right.
Start from i=15
Pick a random number from 1 to i.
Append it to the list.
Swap it with (i-1)th index.
Decrement i by 1.
Repeat the above steps.
The code for above can be:
int[] myList = new int[15];
int[] original_list = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
Random random = new Random();
for (int i = myList.Length; i>=0; i--)
{int randNo = random.Next(0, i-1);
myList[i] = original_list[randNo];
swap(original_list[i-1],original_list[randNo]); //your swap method
}

interviewstreet Triplet challenge

There is an integer array d which does not contain more than two elements of the same value. How many distinct ascending triples (d[i] < d[j] < d[k], i < j < k) are present?
Input format:
The first line contains an integer N denoting the number of elements in the array. This is followed by a single line containing N integers separated by a single space with no leading/trailing spaces
Output format:
A single integer that denotes the number of distinct ascending triples present in the array
Constraints:
N <= 10^5
Every value in the array is present at most twice
Every value in the array is a 32-bit positive integer
Sample input:
6
1 1 2 2 3 4
Sample output:
4
Explanation:
The distinct triplets are
(1,2,3)
(1,2,4)
(1,3,4)
(2,3,4)
Another test case:
Input:
10
1 1 5 4 3 6 6 5 9 10
Output:
28
I tried to solve using DP. But out of 15 test cases only 7 test cases passed.
Please help solve this problem.
You should note that you only need to know the number of elements that are smaller/larger than a particular element to know how many triples it serves as the middle point for. Using this you can calculate the number of triples quite easily, the only remaining problem is to get rid of duplicates, but given that you are limited to at most 2 of the same element, this is trivial.
I solved using a Binary Index Tree http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=binaryIndexedTrees.
I also did a small write up, http://www.kesannmcclean.com/?p=223.
package com.jai;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
public class Triplets {
int[] lSmaller, rLarger, treeArray, dscArray, lFlags, rFlags;
int size, count = 0;
Triplets(int aSize, int[] inputArray) {
size = aSize;
lSmaller = new int[size];
rLarger = new int[size];
dscArray = new int[size];
int[] tmpArray = Arrays.copyOf(inputArray, inputArray.length);
Arrays.sort(tmpArray);
HashMap<Integer, Integer> tmpMap = new HashMap<Integer, Integer>(size);
for (int i = 0; i < size; i++) {
if (!tmpMap.containsKey(tmpArray[i])) {
count++;
tmpMap.put(tmpArray[i], count);
}
}
count++;
treeArray = new int[count];
lFlags = new int[count];
rFlags = new int[count];
for (int i = 0; i < size; i++) {
dscArray[i] = tmpMap.get(inputArray[i]);
}
}
void update(int idx) {
while (idx < count) {
treeArray[idx]++;
idx += (idx & -idx);
}
}
int read(int index) {
int sum = 0;
while (index > 0) {
sum += treeArray[index];
index -= (index & -index);
}
return sum;
}
void countLeftSmaller() {
Arrays.fill(treeArray, 0);
Arrays.fill(lSmaller, 0);
Arrays.fill(lFlags, 0);
for (int i = 0; i < size; i++) {
int val = dscArray[i];
lSmaller[i] = read(val - 1);
if (lFlags[val] == 0) {
update(val);
lFlags[val] = i + 1;
} else {
lSmaller[i] -= lSmaller[lFlags[val] - 1];
}
}
}
void countRightLarger() {
Arrays.fill(treeArray, 0);
Arrays.fill(rLarger, 0);
Arrays.fill(rFlags, 0);
for (int i = size - 1; i >= 0; i--) {
int val = dscArray[i];
rLarger[i] = read(count - 1) - read(val);
if (rFlags[val] == 0) {
update(val);
rFlags[val] = i + 1;
}
}
}
long countTriplets() {
long sum = 0;
for (int i = 0; i < size; i++) {
sum += lSmaller[i] * rLarger[i];
}
return sum;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
int[] a = new int[N];
String[] strs = br.readLine().split(" ");
for (int i = 0; i < N; i++)
a[i] = Integer.parseInt(strs[i]);
Triplets sol = new Triplets(N, a);
sol.countLeftSmaller();
sol.countRightLarger();
System.out.println(sol.countTriplets());
}
}
For tentative algorithm that I came up with, it should be:
(K-1)!^2
where K is number of unique elements.
EDIT
After more thinking about this:
SUM[i=1,K-2] SUM[j=i+1,K-1] SUM[m=j+1,K] 1
=> SUM[i=1,K-2] (SUM[j=i+1,K-1] (K-j))
if the input is not sorted (the question is not clear about this): sort it
remove the duplicated items (this step could be conbined with the first step)
now pick 3 items. Since the items are already sorted, the three chosen items are ordered as well
IIRC there are (n!) / ((n-3)! * 3!) ways to pick the three items; with n := the number of unique items
#hadron: exactly, I couldn get my head around on why it should be 28 and not 35 for a set of 7 distinct numbers *
[Since the ques is about ascending triplets, repeated numbers can be discarded].
btw, here's a very bad Java solution(N^3):
I have also printed out the possible triplets:
I'm also thinking about some function that dictates the no: of triplets possible for input 'N'
4 4
5 10
6 20
7 35
8 56
9 84
package org.HackerRank.AlgoChallenges;
import java.util.Iterator;
import java.util.Scanner;
import java.util.TreeSet;
public class Triplets {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int result = 0;
int n = scanner.nextInt();
Object[] array = new Object[n];
TreeSet<Integer> treeSet = new TreeSet<Integer>();
/*
* for (int i = 0; i < n; i++) { array[i] = scanner.nextInt(); }
*/
while (n>0) {
treeSet.add(scanner.nextInt());
n--;
}
scanner.close();
Iterator<Integer> iterator = treeSet.iterator();
int i =0;
while (iterator.hasNext()) {
//System.out.println("TreeSet["+i+"] : "+iterator.next());
array[i] = iterator.next();
//System.out.println("Array["+i+"] : "+array[i]);
i++;
}
for (int j = 0; j < (array.length-2); j++) {
for (int j2 = (j+1); j2 < array.length-1; j2++) {
for (int k = (j2+1); k < array.length; k++) {
if(array[j]!=null && array[j2]!=null && array[k]!=null){
System.out.println("{ "+array[j]+", "+array[j2]+", "+array[k]+" }");
result++;
}
}
}
}
System.out.println(result);
}
One of solution in python:
from itertools import combinations as comb
def triplet(lis):
done = dict()
result = set()
for ind, num in enumerate(lis):
if num not in done:
index = ind+1
for elm in comb(lis[index:], 2):
s,t = elm[0], elm[1]
if (num < s < t):
done.setdefault(num, None)
fin = (num,s,t)
if fin not in result:
result.add(fin)
return len(result)
test = int(raw_input())
lis = [int(_) for _ in raw_input().split()]
print triplet(lis)
Do you care about complexity?
Is the input array sorted?
if you don't mind about complexity you can solve it in complexity of N^3.
The solution with complexity N^3:
If it not sorted, then sorted the array.
Use 3 for loops one inside the other and go threw the array 3 times for each number.
Use hash map to count all the triples. The key will be the triple it self and the value will be the number of occurences.
It should be something like this:
for (i1=0; i1<N; i1++) {
for (i2=i1; i2<N; i2++) {
for (i3=i2; i3<N; i3++) {
if (N[i1] < N[i2] < N[i3]) {
/* if the triple exists in the hash then
add 1 to its value
else
put new triple to the hash with
value 1
*/
}
}
}
}
Result = number of triples in the hash;
I didn't try it but I think it should work.

Algorithm to iterate through sample space of numbers

I hope this isn't a dupe, but it's hard to boil down the problem into keywords!
This is always something that I've wondered about. Let's say you have a black box that takes n integers as an input (where n > 1). Given that there is a bounds on the integer values, how would you go about writing an algorithm that will push the entire sample space through the black box? (bonus points if n can be specified at runtime)
My attempt when n = 2 is as follows:
int min = 0;
int max = 9;
int a = min;
int b = min;
while(a <= max && b <= max)
{
blackBox(a, b);
a++;
if(a > max)
{
a = min;
b++;
}
}
The above code is fine for two variables, but as you might guess, my algorithm gets really ugly when n approaches double-digits.
Is there a better way to do this other than nesting if statements like I have done?
I know a bad way to do it, which would be to randomly generate the values for each iteration and save the inputs of previous iterations so you don't poke the black box with the same variables twice. However, I was hoping for a more speedy method as collisions really hurt the execution time as the number of unique black box calls approaches (max - min + 1) ^ n
Why not used nested loops? Then you just add more nested loops as necessary.
Might not be overly efficent but you did indicate you need to cover the entire sample space, so you're going to have to run every possible combination of values of the input variables anway - so I doubt there's much you can do about efficency unless it's possible to only evaluate against a portion of the state space.
int min = 0;
int max = 9;
for( int a = min ; a <= max ; ++a )
for( int b = min ; b <= max ; ++b )
blackBox( a , b );
Also, I think you'll find the number of unique calls is (max - min + 1) ^ n, not the other way around.
Edit:
A different run-time version to that already suggested
Imre L seems to have hit the nail on the head for a real-time version using the same language type as your question (something C-like), but since you've tagged this as language agnostic I've decided to try something different (also, I'm learning Python at the moment so was looking for an excuse to practice).
Here's a Python real-time version, in each case x will be a n-tuple, such as [1,0,3,2]. Only thing I will say is this does not include max in the state-space (in the example below it will use 0 to 2 inclusive, not 3) so you'd have to increment max before use.
import itertools
min = 0
max = 3
n = 4
for x in itertools.product(range(min,max), repeat=n):
blackBox( x )
The numbers will be held in array a that will be set dynamically eg: int a[] = new int[n]
If the blackBox cannot be modified to take a sample as array then you can either write an ugly wrapper function for calling it with different count of parameters or you are pretty much out of luck for doing it dynamically.
(Procedural) Pseudo code:
int min = 0;
int max = 9;
int a[] = array();
int count = length(a);
setToMinValue(a);
while(a[count-1] <= max)
{
blackBox(a); // or bb(a[0],a[1],...)
a[0]++;
//while next number needs to be increased
for (int i = 0; a[i] > max && i < count-1; i++) {
a[i] = min;
a[i+1]++;
}
}
Here is a generic solution, in Java:
public class Counter implements Iterator<int[]> {
private int[] max;
private int[] vector;
public Counter(int[] maxValues) {
this.max = maxValues;
this.vector = new int[maxValues.length];
}
public int[] next() {
if (!hasNext())
throw new NoSuchElementException();
int[] res = vector.clone();
int i = 0;
while (i < vector.length && vector[i] == max[i]) {
vector[i] = 0;
i++;
}
if (i == vector.length)
vector = null;
else
vector[i]++;
return res;
}
#Override
public boolean hasNext() {
return (vector != null);
}
#Override
public void remove() {
throw new UnsupportedOperationException();
}
public static void main(String[] args) {
Counter c = new Counter(new int[]{3});
while (c.hasNext()) {
System.out.println(Arrays.toString(c.next()));
}
}
}
The constructor receives the maximum values for each position. The minimum is always 0 (therefore you can use it to simulate a counter in any radix, and in any "mixed radix"). I added a usage example at the bottom.
You may think of each input to the black box as an n-digit number in a max - min + 1 radix system. For example, if min = 3 and max = 12, then max - min + 1 == 10 and each input to the black box corresponds to an n-digit number in the decimal system. Simply iterate over all the numbers from 0 to (max - min + 1)^n, decode each number and feed the resulting vector to the black box.
Here's a Java implementation:
public static interface BlackBox {
void consume(int... vector);
}
public static void iterateSample(int min, int max, int n, BlackBox bb) {
int radix = max - min + 1;
long limit = (long) Math.pow(radix, n); /* Imprecise for larger numbers! */
for (int i = 0; i < limit; i++) {
int encoded = i;
int[] decoded = new int[n];
for (int j = 0; j < n; j++) {
decoded[j] = min + (encoded % radix);
encoded /= radix;
}
bb.consume(decoded);
}
}

Resources