| View previous topic :: View next topic |
| Author |
Message |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Sun Oct 04, 2009 11:19 am Post subject: creating less predictible random? -Java |
|
|
Yea i took Comp sci and have been forced to switch to java for the time being.
| Code: | SecureRandom generator = new SecureRandom();
generator.setSeed(generator.nextInt(2147483647));
generator.setSeed(generator.nextInt(2147483647));
generator.setSeed(generator.nextInt(2147483647));
generator.setSeed(generator.nextInt(2147483647)); |
Basically, would creating a random number generator like that be more secure than just declaring and not setting seeds? In my eyes, it would become harder and harder to find the correct seed with more random generations.
Here's my logic:
Create the generator with an initial random seed based on system time
Take that random generator and generate a number with it, pass this for the seed.
Keep doing that until you are satisfied (if this is more secure i would probably do 100 iterations). _________________
|
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Sun Oct 04, 2009 12:19 pm Post subject: |
|
|
the initial seed is using current time, which is predictable.
Also i'm just doing this for a small school example, no use in using an outside class.
I finished the program he wanted in 5 minutes, and he was like "Well try to make them more random (acting like he can predict them)." _________________
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Sun Oct 04, 2009 12:38 pm Post subject: |
|
|
| blankrider wrote: | | the initial seed is using current time, which is predictable. | If you use the result as the seed, it's predictable too.. That's why they're called pseudo random numbers. The only way to get a more random number is eg. get some RF receiver, record some noise from there and use it. Also, you could record some mouse movement or sth.
| blankrider wrote: | | I finished the program he wanted in 5 minutes, and he was like "Well try to make them more random (acting like he can predict them)." | You could manipulate the distribution a bit. Eg. if your output is limited to certain range, you could do: | Code: | | int result = (int)( ( (double)( rand() )/RAND_MAX )*( max ) ) | or something instead of | Code: | | int result = rand()%max; |
|
|
| Back to top |
|
 |
shhac Expert Cheater
Reputation: 0
Joined: 30 Oct 2007 Posts: 108
|
Posted: Sun Oct 04, 2009 6:09 pm Post subject: |
|
|
you could try to take a random part of the random number too
e.g.
floor((random1 * (10 ^ random2)) % 10)
and repeat that to the number of digits you need to get something like this | Code: | var rand = 0, i = 0, digits = 10;
for(i=0;i<digits;i++){
rand += (Math.floor((Math.random() * Math.pow(10, Math.random())) % 10)) * Math.pow(10,i);
}
rand; |
and you could further overlap this to get something like | Code: | var rand = 0, i = 0, overlap = 3, digits = 10;
for(i=0;i<digits-overlap;i++){
rand += (Math.floor((Math.random() * Math.pow(10, Math.random())) % Math.pow(10,overlap))) * Math.pow(10,i);
}
rand; |
Last edited by shhac on Sun Oct 04, 2009 6:42 pm; edited 3 times in total |
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Sun Oct 04, 2009 6:30 pm Post subject: |
|
|
| Jani wrote: | | blankrider wrote: | | the initial seed is using current time, which is predictable. | If you use the result as the seed, it's predictable too.. That's why they're called pseudo random numbers. The only way to get a more random number is eg. get some RF receiver, record some noise from there and use it. Also, you could record some mouse movement or sth.
| blankrider wrote: | | I finished the program he wanted in 5 minutes, and he was like "Well try to make them more random (acting like he can predict them)." | You could manipulate the distribution a bit. Eg. if your output is limited to certain range, you could do: | Code: | | int result = (int)( ( (double)( rand() )/RAND_MAX )*( max ) ) | or something instead of | Code: | | int result = rand()%max; |
|
I'm trying to understand why
| Code: | | y = (int)( ( (double)( generator.nextInt(100) )/100 )*( 100 ) ); |
will make it more distributed. Care to explain? It looks to me like your just dividing the rand by 100 then multiplying by 100. How does that affect the distribution _________________
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Mon Oct 05, 2009 2:23 am Post subject: |
|
|
| blankrider wrote: | | It looks to me like your just dividing the rand by 100 then multiplying by 100. How does that affect the distribution | First of all, it seems that nextInt limits the upper bound already, so you can forget everything I've said :P Also, it's said that the result is already dirstributed uniformly between 0 and n. So it's better to read the docs before doing any posts. :P I'll explain how the things are in C anyway:
RAND_MAX is the maxium value your rand() will output, eg. 0x7FFF. Do you understand now why it's different?
Let's say you generate 8 bits of random stuff -> max is 255d. If the max you want is 250, to scale it, you need to divide by 255 and then multiply with the max, 250.
Then think a little about the the other method, it'll use the left over, so it isn't distributed equally. Let's say your max is 3 and the maxium number you're getting from rand() is 4: | Code: | 0%3 == 0
1%3 == 1
2%3 == 2
3%3 == 0
4%3 == 1 | See, you're getting twice as much ones and zeroes than twos.. Now do the same with scaling it and see the result.
Okay, and now back to the question. Noticed this while reading the reference: | Code: | | SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); | is said to be completely random unless you call .setSeed(). So I think that could result in a better outcome.. |
|
| Back to top |
|
 |
TheRealLinky How do I cheat?
Reputation: 0
Joined: 18 Jun 2009 Posts: 7
|
Posted: Mon Oct 05, 2009 3:46 pm Post subject: |
|
|
| why not just use your audio in jack as a seed and you pretty much get a nist grade prng. |
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Mon Oct 05, 2009 3:48 pm Post subject: |
|
|
| TheRealLinky wrote: | | why not just use your audio in jack as a seed and you pretty much get a nist grade prng. |
how would i go about that? I'm pretty new to java  _________________
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Tue Oct 06, 2009 5:14 am Post subject: |
|
|
| TheRealLinky wrote: | | why not just use your audio in jack as a seed and you pretty much get a nist grade prng. | Just make sure no microphone is plugged in and that the jack is really working... |
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Tue Oct 06, 2009 7:59 pm Post subject: |
|
|
how would i use that as a seed? I dont know how to get a handle to it in java. _________________
|
|
| Back to top |
|
 |
smartz993 I post too much
Reputation: 2
Joined: 20 Jun 2006 Posts: 2013 Location: USA
|
Posted: Tue Oct 06, 2009 9:51 pm Post subject: |
|
|
| Code: | import javax.sound.sampled.*;
import java.io.*;
public class Test {
public static void main(String args[])
{
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
AudioFormat audioFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
44100.0F, 16, 2, 4, 44100.0F, false);
Port.Info portInfo = new Port.Info(TargetDataLine.class,
Port.Info.SPEAKER.getName(), false);
DataLine.Info dataLineInfo = new
DataLine.Info(portInfo.getLineClass(),
audioFormat);
Mixer mixer = AudioSystem.getMixer(mixerInfo[3]);
TargetDataLine targetDataLine = null;
try {
targetDataLine = (TargetDataLine)mixer.getLine(dataLineInfo);
targetDataLine.open(audioFormat);
} catch ( Exception e) { System.out.println("E: "+e); }
int numBytesAvailable = targetDataLine.available();
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String exitQ = null;
targetDataLine.start();
while(true) {
byte tempBuffer[] = new byte[10000];
targetDataLine.read(tempBuffer, 0, tempBuffer.length);
System.out.println(tempBuffer.toString());
System.out.println("# bytes available = "+numBytesAvailable);
try {
exitQ = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error trying read exit code!");
System.exit(1);
}
if (exitQ.charAt(0)=='e') { System.out.println("Goodbye");
System.exit(1); } //type e to exit
}
}
|
Try something like that?
- From bottom of: http://www.techtalkz.com/java/104007-reading-sound-card-output.html |
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Fri Oct 09, 2009 4:32 am Post subject: |
|
|
distribution wise, java's number generator is spot on (at least in my context). Using a test of 10000 * 100 numbers, there is about a 9.94 - 10.06% distribution (with 10 being perfect, and possible numbers from 1-10).
In the end i used SHA1PRNG algorithm in SecureRandom. Note this was for a simple java project in school and i was trying to somewhat stick to the classes in informed everyone to use (just plain Random) _________________
|
|
| Back to top |
|
 |
TheRealLinky How do I cheat?
Reputation: 0
Joined: 18 Jun 2009 Posts: 7
|
Posted: Fri Oct 09, 2009 2:25 pm Post subject: |
|
|
| If that was the case id have gone with the NIST grade seed to show off. |
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Fri Oct 09, 2009 9:38 pm Post subject: |
|
|
| TheRealLinky wrote: | | If that was the case id have gone with the NIST grade seed to show off. |
Too much effort lol. I already showed up the teacher on the assignment by proving his way was not more random than mine. What a froob _________________
|
|
| Back to top |
|
 |
|