Results 1 to 12 of 12
  1. #1
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Random-like class seeded with byte[]

    Is there any variation of java.util.Random that can be seeded with an array of bytes rather than just a long? That is, a library anywhere that anyone knows of? Couldn't seem to find one on google. :|

    I need to maintain long seeds as part of an evolution algorithm.

  2. #2
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Random-like class seeded with byte[]

    Well, I made my own I just hope it works well enough. If anyone knows of a library though, please let me know.

    Java Code:
    public class GeneticIterator
    {
    	private final byte[] data;
    	private int index;
    	
    	public GeneticIterator(byte[] data)
    	{
    		if(data.length <= 0)
    			throw new IllegalArgumentException("Array size must be positive");
    		this.data = data;
    		index = 0;
    	}
    	
    	private void wrap()
    	{
    		while(index >= data.length)
    			index -= data.length;
    	}
    	
    	public void consume(int n)
    	{
    		if(n < 0)
    			throw new IllegalArgumentException("n must be non-negative");
    		index += n;
    		wrap();
    	}
    	
    	public boolean nextBoolean()
    	{
    		boolean bool = data[index++] < 0;
    		wrap();
    		return bool;
    	}
    	
    	public byte nextByte()
    	{
    		byte b = data[index++];
    		wrap();
    		return b;
    	}
    	
    	public byte[] nextBytes(int n)
    	{
    		byte[] data = new byte[n];
    		for(int i = 0; i < data.length; i++)
    		{
    			data[i] = nextByte();
    			wrap();
    		}
    		return data;
    	}
    	
    	public short nextShort()
    	{
    		return ByteBuffer.wrap(nextBytes(Short.SIZE)).getShort();
    	}
    	
    	public char nextChar()
    	{
    		return ByteBuffer.wrap(nextBytes(Character.SIZE)).getChar();
    	}
    	
    	public int nextInt()
    	{
    		return ByteBuffer.wrap(nextBytes(Integer.SIZE)).getInt();
    	}
    	
    	public long nextLong()
    	{
    		return ByteBuffer.wrap(nextBytes(Long.SIZE)).getLong();
    	}
    	
    	public float nextFloat()
    	{
    		return ByteBuffer.wrap(nextBytes(Float.SIZE)).getFloat();
    	}
    	
    	public double nextDouble()
    	{
    		return ByteBuffer.wrap(nextBytes(Double.SIZE)).getDouble();
    	}
    }

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Random-like class seeded with byte[]

    Quote Originally Posted by AndrewM16921 View Post
    Is there any variation of java.util.Random that can be seeded with an array of bytes rather than just a long? That is, a library anywhere that anyone knows of? Couldn't seem to find one on google. :|

    I need to maintain long seeds as part of an evolution algorithm.
    Define a hash function (method) that creates a long value given a byte array and use the long value for the seed of the pseudo random number generator.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Random-like class seeded with byte[]

    Ah, good idea. Thanks! :)

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Random-like class seeded with byte[]

    Maybe the (static) method hashCode(byte[] a) in the Arrays class can be of help; read its API documentation.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: Random-like class seeded with byte[]

    Quote Originally Posted by JosAH View Post
    Maybe the (static) method hashCode(byte[] a) in the Arrays class can be of help.
    Since this returns an int rather than a long there is a loss of entropy. I would convert 4 bytes of a digest (it should not matter which 4) of the byte array to an long and use that as the seed. Since one is in effect truncating the digest to 4 bytes then it should not matter whether one uses MD5, SHA1, SHA2 or any of the established digest algorithm.

  7. #7
    AndrewM16921 is offline Senior Member
    Join Date
    Jan 2009
    Location
    NJ, USA
    Posts
    263
    Rep Power
    6

    Default Re: Random-like class seeded with byte[]

    Quote Originally Posted by sabre150 View Post
    Since this returns an int rather than a long there is a loss of entropy. I would convert 4 bytes of a digest (it should not matter which 4) of the byte array to an long and use that as the seed. Since one is in effect truncating the digest to 4 bytes then it should not matter whether one uses MD5, SHA1, SHA2 or any of the established digest algorithm.
    That's precisely what I ended up doing. :)

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Random-like class seeded with byte[]

    Quote Originally Posted by sabre150 View Post
    Since this returns an int rather than a long there is a loss of entropy.
    Golly, you're right; when I first read the API I mistakenly read a 'long' for the return type; it must've been a form of 'wishful reading' ... sorry about that.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: Random-like class seeded with byte[]

    Quote Originally Posted by JosAH View Post
    Golly, you're right;
    Except that I have been working with the Arduino for the last couple of months where a long is 4 bytes but of course in Java a long is 8 bytes so I should have said select 8 bytes from the digest and not 4! Silly me.

  10. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Random-like class seeded with byte[]

    Quote Originally Posted by sabre150 View Post
    Except that I have been working with the Arduino for the last couple of months where a long is 4 bytes but of course in Java a long is 8 bytes so I should have said select 8 bytes from the digest and not 4! Silly me.
    An Arduino? What a coincidence: I have an Arduino 2560 rigth in front of me: all pooped up with a vague wifi antenna, a couple of triacs and relais, two thermometers and a whole lot of wires ;-) (still waiting for a light meter and a humidity meter to arrive). I'm building something for home automation; it's fun (I use Java for the PC part of the entire application, so this reply is on topic).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: Random-like class seeded with byte[]

    :-))))))) Interesting - I have a Mega 2560 connected via a USB to my main computer; cannot yet connect via WIFI. At the moment I am concentrating on bootstrapping my minimal test equipment (Power supplies + very old and flakey Tektronics D755) to a more usable set. I am currently using the Mega to create a signal generator using an AD9850 module and a frequency measurement system using the Mega's timers. Still crude but, apart from 'de-bouncing' problems, works well.

    I also have a Raspberry Pi but have yet to find a real use for it. I can talk to hardware with it but to my mind it is much more limited than Mega 256. I'm hoping they will complement one another but at the moment I don't see the best way to use the combination.

    One background project is to use the Mega to create an HSM. Still very much in it's infancy but very very doable.

    I also use Java as a front end to the Mega so yes - still just about on topic!

    P.S. Jos - I have sent you a private message.
    Last edited by sabre150; 01-02-2013 at 10:44 AM.

  12. #12
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Random-like class seeded with byte[]

    I only use those timers to dim a couple of triacs (that's a breeze); I hate it that the USB circuit resets that little Arduino (nothing a little soldering won't solve) but RXTX is ideal for communication purposes. I sent you a link to that WiFi antenna I'm using (I quit using the 'official' and expensive ethernet shield + a little wireless router). That Raspberry thingie is a little wonder i.m.h.o.

    kind regards,

    Jos (still on topic because I mentioned RXTX ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 04-21-2010, 02:28 AM
  2. Replies: 14
    Last Post: 10-19-2009, 10:57 AM
  3. Byte class ?
    By Hardik in forum New To Java
    Replies: 1
    Last Post: 07-20-2009, 03:34 PM
  4. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •