Sunday, July 8, 2012

Near Random number… 2

In continuation to my previous post.. i made a few more refinements and made the program more simple..

  • The advantage of this new one is it allows the total digits we want to alter w.r.t the previous one.
  • we can dictate the number of variants between consecutive integers

it reads as follows

import java.util.Random;

public class NearRandomIncrements2 {
private static final Random rand = new Random(99L);
private static final int maxVariants = 10;
private static final int maxVariableDigits = 10000;
private static int[] variantArr = new int[maxVariants];
private static int[] cummulativeArr = new int[maxVariants];
private static int sum;

static {
for (int i = 0; i < maxVariants; i++) {
int nextVariant = Math.abs(rand.nextInt()) % maxVariableDigits;
variantArr[i] = nextVariant;
sum += nextVariant;
cummulativeArr[i] = sum;
System.out.println(">>" + nextVariant + " cumm:" + cummulativeArr[i]);
}
}

public static void main(String[] s) {
long prev = 0;
for (long i = 0L; i < 40; i++) {
int index = (int) (i % (maxVariants));
long cycles = (i / (maxVariants)) + 1;
long newVal = cycles + cummulativeArr[index] + index;
if (newVal < 0) {
newVal = Long.MAX_VALUE + newVal + 1;
}
// if (newVal >= 0 && newVal < 100) {
System.out.println(i + " new no:" + newVal + " cycle:" + cycles + " variant:" + (newVal - prev));
// }
prev = newVal;
}
}
}



For the given maxVariable digits the random numbers generated are as follows

>>5722 cumm:5722
>>6916 cumm:12638
>>4859 cumm:17497
>>1273 cumm:18770
>>696 cumm:19466
>>3525 cumm:22991
>>5948 cumm:28939
>>9283 cumm:38222
>>5856 cumm:44078
>>9198 cumm:53276



The numbers remain the same on multiple runs also.

0 new no:5723 cycle:1 variant:5723
1 new no:12640 cycle:1 variant:6917
2 new no:17500 cycle:1 variant:4860
3 new no:18774 cycle:1 variant:1274
4 new no:19471 cycle:1 variant:697
5 new no:22997 cycle:1 variant:3526
6 new no:28946 cycle:1 variant:5949
7 new no:38230 cycle:1 variant:9284
8 new no:44087 cycle:1 variant:5857
9 new no:53286 cycle:1 variant:9199
10 new no:5724 cycle:2 variant:-47562
11 new no:12641 cycle:2 variant:6917
12 new no:17501 cycle:2 variant:4860
13 new no:18775 cycle:2 variant:1274
14 new no:19472 cycle:2 variant:697
15 new no:22998 cycle:2 variant:3526
16 new no:28947 cycle:2 variant:5949
17 new no:38231 cycle:2 variant:9284
18 new no:44088 cycle:2 variant:5857
19 new no:53287 cycle:2 variant:9199
20 new no:5725 cycle:3 variant:-47562
21 new no:12642 cycle:3 variant:6917
22 new no:17502 cycle:3 variant:4860
23 new no:18776 cycle:3 variant:1274
24 new no:19473 cycle:3 variant:697
25 new no:22999 cycle:3 variant:3526
26 new no:28948 cycle:3 variant:5949
27 new no:38232 cycle:3 variant:9284
28 new no:44089 cycle:3 variant:5857
29 new no:53288 cycle:3 variant:9199
30 new no:5726 cycle:4 variant:-47562
31 new no:12643 cycle:4 variant:6917
32 new no:17503 cycle:4 variant:4860
33 new no:18777 cycle:4 variant:1274
34 new no:19474 cycle:4 variant:697
35 new no:23000 cycle:4 variant:3526
36 new no:28949 cycle:4 variant:5949
37 new no:38233 cycle:4 variant:9284
38 new no:44090 cycle:4 variant:5857
39 new no:53289 cycle:4 variant:9199
if u observe no two numbers are related to each other; and to know the sequence we need to see atleast 30 consecutive numbers (say we have maxVariant by 10 numbers) So by putting the maxVariants a high number and giving the gerated numbers to a trusted client we may use this algo to generate proxies not loosing even one number in the seq bts 0-> Long.Max_Value
any better soln?

No comments:

Post a Comment