Random Number Generator Question

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 1
Reputation: Matthew85 is an unknown quantity at this point 
Solved Threads: 0
Matthew85 Matthew85 is offline Offline
Newbie Poster

Random Number Generator Question

 
0
  #1
Oct 23rd, 2008
I am writting a program that takes in a number that ranges from 0-255 from an input file and a chip number, then I have to do a CDMA encoding on it. I am having a couple problems with the seeded random number generator. I need to have a 4 bit number generator for each bit read in and it keeps giving me problems. Below is the code that I have created. Thank you for the help.

  1. try{
  2. inputFile = br.readLine();
  3. File f;
  4. f=new File(inputFile);
  5. String strLine = null;
  6. BufferedReader iFile = new BufferedReader (new FileReader(f));
  7.  
  8. while ((strLine = iFile.readLine()) != null) {
  9. // The value taken from each line of the input file
  10. float fl = Float.valueOf(strLine.trim()).floatValue();
  11. // Converted to int so that it floors the value
  12. int i = (int)fl;
  13. String st = "";
  14. st = Integer.toBinaryString((int)i);
  15.  
  16. String string = "";
  17. String bString = "";
  18. System.out.println("st = " + st);
  19. // Reads each bit
  20. for (int q = 0; q < st.length(); q++){
  21. char cd = st.charAt(q);
  22.  
  23. string = String.valueOf(cd);
  24. int c = Integer.parseInt(string);
  25. l = Long.parseLong(seed.trim());
  26.  
  27. // Inverts the chip number if the input bit is 0
  28. if (cd == '1'){
  29. bString = bString + chipNumber;
  30. }
  31. if (cd == '0'){
  32. bString = bString + chipInverse;
  33. }
  34.  
  35. //declare a new instance of the Random class
  36.  
  37. Random aRandom = new Random();
  38. aRandom.setSeed(l);
  39.  
  40.  
  41.  
  42. int out = aRandom.nextInt(c);
  43.  
  44. String outString = Integer.toBinaryString((int) out);
  45. String finalString = "";
  46. finalString = finalString + outString;
  47.  
  48. }
  49.  
  50. }
  51. } catch (IOException ioe) {
  52. System.out.println("IO error trying to read input file");
  53. System.exit(1);
  54. }
Last edited by Matthew85; Oct 23rd, 2008 at 10:39 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 34
Reputation: brianlevine is an unknown quantity at this point 
Solved Threads: 5
brianlevine brianlevine is offline Offline
Light Poster

Re: Random Number Generator Question

 
0
  #2
Oct 24th, 2008
It's been a while since I used java random number generator, so hopefully someone will correct me if I'm wrong, but I believe if you use the same seed for the Random number generator you'll get the same output each time. Is this what you want?

What's the problem you're seeing?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC