Monday, July 23, 2012

Generating gradiants with out using images


This is written in JQuery. Below is the code.


<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" > </script>

<script>
$(function(){
gradiant('div.newGradiant',[255,0,0],[0,0,0]);
});

function gradiant(selector,rgb1,rgb2){
var $src = $(selector);
var height = $src.height()*1;
$src.css('background','yellow');
var x = $src.offset().left + $src.width();
var y = $src.offset().top;
//var r=256,g=256;b=256;
var r=rgb1[0],g=rgb1[1];b=rgb1[2];
var rr=rgb2[0],gg=rgb2[1];bb=rgb2[2];
for(var i=0;i<height;i++){
var tr= Math.round(r+((rr-r)*i/height));
var tg=Math.round(g+((gg-g)*i/height));
var tb=Math.round(b+((bb-b)*i/height));
var newColor = hex(tr)+hex(tg)+hex(tb);
var div =('<DIV style="BORDER-BOTTOM: #ffffff 0px;'
+' BORDER-LEFT: #ffffff 0px solid; BACKGROUND-COLOR: '
+'#'+ newColor +'; MIN-HEIGHT: 1px; HEIGHT:'
+' 1px; FONT-SIZE: 1px; OVERFLOW: hidden; BORDER-TOP: '
+'#ffffff 0px; BORDER-RIGHT: #ffffff 0px solid"></DIV>');
$src.append($(div));
}
}

var hexDigits = new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"); 

function hex(x) {
return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
}

</script>

<div class="newGradiant" style="width:100px; height:200px; background:yellow; border: 1px solid black">

 
This generates the gradiant as follows. The algo can be modified to take the edge rgb values and then do the magic. This is just a naive example ;)
Tested this on IE-7, IE-8, IE-9. Chrome 20.0, firefox 13.0 version.



image


The only issue i see is when we combine this with jQuery corner apis. The results are working in IE but not in chrome!!!

eclipse: deploying classpath jars into tomcat container

Aaaah! this is a big pain.. u will have all jars in classpath, and when u say run as server they dont get copied to the server. the Fix is simpl ( eclipse should have taken care of it by default.. however.. ).

  1. click on the poject,
  2. press ctrl + enter.
  3. Now u see a properties window of the project
  4. search for deployment assemble and click on it.
  5. Now in web-deployment assembly plane click on add button
  6. select java build path entities
  7. click on next. now select all jars (ctrl + a); then OK
  8. now click finish.

Deploy the stuff on tomcat and u will see classpath entities are being copied to the server

image

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?

Thursday, July 5, 2012

Near random–Converting a sequence no to random no

Q: so the statement of the problem is as follows. I want to generate unique numbers from 1 to Max (say max is Long.MAX_LONG). The generated sequence should not be predictable. SO it means we need to generate uniq random nubers and ensuer they dont repeate. Remember we cannot use current time stamp + some increment number as we have limited range and we need to use it as affectively as possible.

So i wrote a func that transforms a function to (seemingly) random number. Its as follows.

public class NearRandomIncrements {
private static final long salt = 0xABCDEF12345628ACL;
private static final long salt2 = 0xeE72CC072837198DL;

public static void main(String[] s) {
long prev = 0;
for (long i = 1L; i > 0 && i < Long.MAX_VALUE; i++) {
long withSalt = Long.reverseBytes(Long.rotateLeft(i, 31) ^ salt);
long withSalt2 = Long.rotateLeft(withSalt, 17) ^ salt2;
long variation = withSalt2 - prev;
prev = withSalt2;
System.out.println(i + " " + withSalt2 + " variation:" + Math.abs(variation));
}
}
}



This genates the output as follows

1 4835434266867286493 variation:4835434266867286493
2 4763378871852614109 variation:72055395014672384
3 4835436465890542045 variation:72057594037927936
4 4763381070875869661 variation:72055395014672384
5 4835438664913797597 variation:72057594037927936
6 4763383269899125213 variation:72055395014672384
7 4835440863937053149 variation:72057594037927936
8 4763367876736336349 variation:72072987200716800
9 4835425470774264285 variation:72057594037927936
10 4763370075759591901 variation:72055395014672384

So if u closely observe based on variation we see a pattern; and we can predict the next number. But if u see the i=8 part; we see a new variation creeping in. So even if a person gets hold of the sequence he may predict only limited number of numbers only. The solution may not be perfect, but i think its a gud start. What do u think?