DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Random Number Generator Question (http://www.daniweb.com/forums/thread153186.html)

Matthew85 Oct 23rd, 2008 10:30 pm
Random Number Generator Question
 
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.

 try{
              inputFile = br.readLine();
          File f;
          f=new File(inputFile);
          String strLine = null;
          BufferedReader iFile = new BufferedReader (new FileReader(f));
         
          while ((strLine = iFile.readLine()) != null)  {
                  // The value taken from each line of the input file
                  float fl = Float.valueOf(strLine.trim()).floatValue();
                  // Converted to int so that it floors the value
                  int i = (int)fl;
                  String st = "";
                  st = Integer.toBinaryString((int)i);
               
                  String string = "";
                  String bString = "";
                  System.out.println("st = " + st);
                  // Reads each bit
                  for (int q = 0; q < st.length(); q++){
                          char cd = st.charAt(q);
                         
                          string = String.valueOf(cd);
                          int c = Integer.parseInt(string);
                          l = Long.parseLong(seed.trim());
                       
                          // Inverts the chip number if the input bit is 0
                          if (cd == '1'){
                                  bString = bString + chipNumber;
                          }
                          if (cd == '0'){
                                  bString = bString + chipInverse;
                          }
                         
                          //declare a new instance of the Random class
                       
                                  Random aRandom = new Random();
                                  aRandom.setSeed(l);
                         
                           
                         
                            int out = aRandom.nextInt(c);
                           
                            String outString = Integer.toBinaryString((int) out);
                            String finalString = "";
                            finalString = finalString + outString;
                           
                }
                         
                  }
      } catch (IOException ioe) {
          System.out.println("IO error trying to read input file");
          System.exit(1);
      }

brianlevine Oct 24th, 2008 1:47 am
Re: Random Number Generator Question
 
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?


All times are GMT -4. The time now is 12:09 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC