Results 1 to 12 of 12
- 01-01-2013, 02:01 AM #1
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
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.
- 01-01-2013, 02:20 AM #2
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
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(); } }
- 01-01-2013, 02:14 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Re: Random-like class seeded with byte[]
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-01-2013, 07:52 PM #4
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
Re: Random-like class seeded with byte[]
Ah, good idea. Thanks! :)
- 01-01-2013, 08:18 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-01-2013, 11:58 PM #6
Member
- Join Date
- Jul 2012
- Location
- Earth
- Posts
- 75
- Rep Power
- 0
Re: Random-like class seeded with byte[]
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.
- 01-02-2013, 07:40 AM #7
Senior Member
- Join Date
- Jan 2009
- Location
- NJ, USA
- Posts
- 183
- Rep Power
- 5
- 01-02-2013, 08:46 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Re: Random-like class seeded with byte[]
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-02-2013, 09:48 AM #9
Member
- Join Date
- Jul 2012
- Location
- Earth
- Posts
- 75
- Rep Power
- 0
- 01-02-2013, 10:06 AM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Re: Random-like class seeded with byte[]
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-02-2013, 10:29 AM #11
Member
- Join Date
- Jul 2012
- Location
- Earth
- Posts
- 75
- Rep Power
- 0
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.
- 01-02-2013, 11:01 AM #12
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
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 ;-)When people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Streaming an image byte by byte (and similtaneosly rendering it on screen)
By ea25 in forum New To JavaReplies: 1Last Post: 04-21-2010, 02:28 AM -
how i use the random class to random the cards i have
By yanipao in forum New To JavaReplies: 14Last Post: 10-19-2009, 10:57 AM -
Byte class ?
By Hardik in forum New To JavaReplies: 1Last Post: 07-20-2009, 03:34 PM -
How do I generate random numbers in a certain range using the random class?
By frasifrasi in forum New To JavaReplies: 8Last Post: 04-19-2009, 05:50 PM -
random numbers without random class`
By carlos123 in forum New To JavaReplies: 4Last Post: 01-17-2008, 10:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks