| In Chapter 7: Tech 
            : Generating Non-uniform Distributions we discussed the transformation 
            and rejection methods 
            for generating random distributions. The transformation method provides 
            for those distributions that follow a behaved analytical function 
            while the rejection method can provide for any type of distribution, 
            even custom designed ones with discontinuities and gaps. So where might one need to use such a custom probability 
              distribution? One situation where they come in a handy is in in 
              complex, multi-part experiment simulations.  For example, an experiment at a particle accelerator 
              might need one part of the program to simulate the beam and its 
              interactions with a target. A second part of the program would follow 
              scattered particles from the interaction into the detector. The 
              detector, in turn, is usually a complex of many types of detectors, 
              each of which needs a unique simulation program.  If we want to study one of these sub-detector simulators 
              in isolation, we usually don't want to run the full beam simulator 
              and the tracking of the particles before they reach the sub-detector. 
              This will slow down the processing considerably. Instead, we can 
              eliminate those parts and just generate the particles entering the 
              detector with, say, angles produced according to a custom distribution 
              that matches well with that produced by the full simulation.    
               
                 
                   
                    Figure Phy.7.1 Diagram of an experiment at a particle 
                      accelerato showing a cross section of the target and detector 
                      elements. The particle beam goes left to right and passes 
                      through a target where occasionally one of the beam particles 
                      collides with a particle such as a proton in the target. 
                      The detector system measures the tracks of particles in 
                      the debris of the collisison. Most of the beam goes right 
                      through the target and is typically too intense to place 
                      a detector directly in the beam. In that case, the detector 
                      will only cover part of the forward area and will miss some 
                      of the debris particles. Suppose we want to study out detectors and we want to distribute 
              the debris tracks in a distribution in angles similar to what would 
              occur in the experiment. Below we show a  
  
               
                 
                   
                    
                       Figure Phy.7.2 A fictional plot of how the particles 
                        might be distributed in forward angle (with respect to 
                        the beamline) for a sample of the detector elements. The 
                        bars show the ranges of angles covered by each of the 
                        6 detector elements. From the plot we can see that the distribution for a detector element 
              would rise towards the beamline. We can use the rejection 
              method to create a sloped distribution of angles values. In 
              the applet below we   
              
                 
                  |  
                      RanDistAnglesApplet.java 
                        - This program creates a random distribution of angles 
                        as might be seen for tracks entering a detector element 
                        in a particle scattering experiment.
 + Previous classes:
 Chapter 
                        6:Tech: Histogram.java, 
                        HistPanel.java
 Chapter 
                        6:Tech: PlotPanel.java, 
                        PlotFormat.java
 
 |   
                  |  import 
                      javax.swing.*;import java.awt.*;
 import java.awt.event.*;
 
 /**
 *  This program creates a random distribution 
                      of angles to
 *  artificially simulate the tracks 
                      in a particle scattering
 *  experiment entering a detector 
                      element.
 *
 *  The applet uses the HistPanel to 
                      display contents of
 *  an instance of Histogram.
 *
 *  Includes "Go" button to add random 
                      values from a Gaussian
 *  distribution to the histogram. 
                      The number of values taken from
 *  entry in a JTextField. "Clear"  button 
                      clears the histogram.
 *  In standalone mode, the Exit button 
                      closes the program.
 *
 *  This program will run as an applet 
                      inside
 *  an application frame.
 *
 **/
 public class RanDistAnglesApplet extends JApplet
 implements ActionListener
 {
 // Use the HistPanel JPanel subclass here
 HistPanel fOutputPanel;
 
 Histogram fHistogram;
 int fNumDataPoints = 20000;
 
 // Set the angular range for scattered tracks
 double fMinAngle;
 double fMaxAngle;
 
 // Random number generator
 java.util.Random fRan;
 
 // A text field for input strings
 JTextField fTextField;
 
 // Flag for whether the applet is in a browser
 // or running via the main () below.
 boolean fInBrowser = true;
 
 //Buttons
 JButton fGoButton;
 JButton fClearButton;
 JButton fExitButton;
 
 /**
 * Create a User Interface with histograms 
                      and buttons to
 * control the program. A textfield holds 
                      number of entries
 * to be generated for the histogram.
 **/
 public void init () {
 
 JPanel panel = new JPanel (new BorderLayout 
                      ());
 
 // Create a histogram with Gaussian 
                      distribution.
 makeHist ();
 
 // JPanel subclass here.
 fOutputPanel = new HistPanel (fHistogram);
 
 panel.add (fOutputPanel,"Center");
 
 // Use a textfield for an input 
                      parameter.
 fTextField =
 new JTextField (Integer.toString 
                      (fNumDataPoints), 10);
 
 // If return hit after entering 
                      text, the
 // actionPerformed will be invoked.
 fTextField.addActionListener (this);
 
 fGoButton = new JButton ("Go");
 fGoButton.addActionListener (this);
 
 fClearButton = new JButton ("Clear");
 fClearButton.addActionListener (this);
 
 fExitButton = new JButton ("Exit");
 fExitButton.addActionListener (this);
 
 JPanel control_panel = new JPanel 
                      ();
 
 control_panel.add (fTextField);
 control_panel.add (fGoButton);
 control_panel.add (fClearButton);
 control_panel.add (fExitButton);
 
 if (fInBrowser) fExitButton.setEnabled 
                      (false);
 
 panel.add (control_panel,"South");
 
 // Add text area with scrolling 
                      to the contentPane.
 add (panel);
 
 } // init
 
 public void actionPerformed (ActionEvent e) 
                      {
 Object source = e.getSource ();
 if (source == fGoButton || source 
                      == fTextField) {
 String strNumDataPoints 
                      = fTextField.getText ();
 try {
 fNumDataPoints 
                      = Integer.parseInt (strNumDataPoints);
 }
 catch (NumberFormatException 
                      ex) {
 // Could 
                      open an error dialog here but just
 // display 
                      a message on the browser status line.
 showStatus 
                      ("Bad input value");
 return;
 }
 makeHist ();
 repaint ();
 }
 else if ( source == fClearButton  ) 
                      {
 fHistogram.clear 
                      ();
 repaint 
                      ();
 } else if (!fInBrowser)
 System.exit 
                      (0);
 } // actionPerformed
 
 /**
 * Create a random distribution of angles 
                      to
 * simulate the tracks in a particle scattering
 * experiment entering a detector element.
 **/
 void makeHist () {
 // Create an instance of the Random 
                      class for
 // producing our random values.
 fRan = new java.util.Random ();
 
 // Could modify the program to select 
                      these via the GUI
 fMinAngle = 25.0;
 fMaxAngle = 50.0;
 
 // Create an instance of our basic 
                      histogram class.
 // Make it wide enough enough to 
                      include most of the
 // gaussian values.
 if (fHistogram == null)
 fHistogram = new Histogram 
                      ("Scatter Angles into a Detector",
 "Degrees",
 25,fMinAngle,fMaxAngle);
 
 // Use the transformation method 
                      to generate a
 // radioactive decay distribution
 for (int i=0; i < fNumDataPoints; 
                      i++) {
 // Generate random vals 
                      0.0 to 1.0
 double r1 = fRan.nextDouble 
                      ();
 double angle = fMinAngle 
                      +  (fMaxAngle-fMinAngle)*r1;
 if (acceptAngle (angle) 
                      ) {
 fHistogram.add 
                      (angle);
 }
 }
 } // makeHist
 
 /**
 * This rejection function uses a simple 
                      linear
 * function to determine the acceptance 
                      of the
 * angle.
 **/
 boolean acceptAngle (double angle){
 if (angle < fMinAngle || angle > 
                      fMaxAngle) return false;
 double c = 0.2;
 double slope = 0.032;
 
 double limit = c +  (angle 
                      - fMinAngle) * slope;
 double r2 = fRan.nextDouble ();
 if ( r2 <= limit) return true;
 return false;
 
 } // acceptAngle
 
 /** Can run applet as an application. **/
 public static void main (String[] args) {
 //
 int frame_width=450;
 int frame_height=300;
 
 // Create the applet and add it 
                      to a frame.
 RanDistAnglesApplet applet = new 
                      RanDistAnglesApplet ();
 applet.fInBrowser = false;
 applet.init ();
 
 // Following anonymous class used 
                      to close window & exit program
 JFrame f = new JFrame ("Demo");
 f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
 
 // Add applet to the frame
 f.getContentPane ().add ( applet);
 f.setSize (new Dimension (frame_width,frame_height));
 f.setVisible (true);
 
 } // main
 
 } // RanDistAnglesApplet
 |  Why would you ever need to use non-uniform distributions? Perhaps 
              such an angle distributions might be used to study the effects of 
              radiation damage to sensitive areas of the detector that are closer 
              to the beam line and therefore receive higher dosages.  References & Web Resources Last Update: Oct. 22, 2005 |