Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with random number test 1

Status
Not open for further replies.

hummus

Programmer
Jul 2, 2008
15
US

Hi, I am making a program that generates random numbers between 0 and 1, and then displays the number of randoms in each appropriate bin (the bin sizes are 0.05).

I'm thinking that I would use something like "bin1 ++" to add a count to the bin, but I'm not sure. My code is below. I am using FORTRAN 77.

Thanks for your help!

Code:
 PROGRAM random_number
!** program to test a random number generator
        IMPLICIT NONE
                                                                                                                                                             
        INTEGER :: seed, a
        INTEGER :: bin1, bin2, bin3, bin4, bin5, bin6, bin7, bin8, bin9, bin10, bin11, bin12, bin13, bin14, bin15, bin16,
        &          bin17, bin18, bin19, bin20
        PARAMETER (seed = 86456)
        REAL :: x, rand
                                                                                                                                                             
        CALL SRAND(seed)
                                                                                                                                                             
        DO a=1, 20000
                x = rand(a) !*** assigns number between 0 and 1 to variable X
                                                                                                                                                             
                IF (x > 0 .AND. x <= 0.05) THEN
                  !**add one count to bin1
                ELSEIF (x > 0.05 .AND. x <= 0.10) THEN
                  !**add one count to bin2
                ELSEIF (x > 0.10 .AND. x <= 0.15) THEN
                  !**add one count to bin3
                ELSEIF (x > 0.15 .AND. x <= 0.20) THEN
                  !**add one count to bin4
                ELSEIF (x > 0.20 .AND. x <= 0.25) THEN
                  !**add one count to bin5
                ELSEIF (x > 0.25 .AND. x <= 0.30) THEN
                  !**add one count to bin6
                ELSEIF (x > 0.30 .AND. x <= 0.35) THEN
                  !**add one count to bin7
                ELSEIF (x > 0.35 .AND. x <= 0.40) THEN
                  !**add one count to bin8
                ELSEIF (x > 0.40 .AND. x <= 0.45) THEN
                  !**add one count to bin9
                ELSEIF (x > 0.45 .AND. x <= 0.50) THEN
                  !**add one count to bin10
                ELSEIF (x > 0.50 .AND. x <= 0.55) THEN
                  !**add one count to bin11
                ELSEIF (x > 0.55 .AND. x <= 0.60) THEN
                  !**add one count to bin12
                ELSEIF (x > 0.60 .AND. x <= 0.65) THEN
                  !**add one count to bin13
                ELSEIF (x > 0.65 .AND. x <= 0.70) THEN
                  !**add one count to bin14
                ELSEIF (x > 0.70 .AND. x <= 0.75) THEN
                  !**add one count to bin15
                ELSEIF (x > 0.75 .AND. x <= 0.80) THEN
                  !**add one count to bin16
                ELSEIF (x > 0.80 .AND. x <= 0.85) THEN
                  !**add one count to bin17
                ELSEIF (x > 0.85 .AND. x <= 0.90) THEN
                  !**add one count to bin18
                ELSEIF (x > 0.90 .AND. x <= 0.95) THEN
                  !**add one count to bin19
                ELSEIF (x > 0.95 .AND. x <= 1.00) THEN
                  !**add one count to bin20
                ENDIF
                                                                                                                                                             
                WRITE(*,*) bin1, bin2, bin3, bin4, bin5, bin6, bin7, bin8, bin9, bin10, bin11, bin12, bin13, bin14, bin15, 
        &                  bin16, bin17, bin18, bin19, bin20
        END DO
                                                                                                                                                             
        END PROGRAM random_number
 
You are making it awfully hard on yourself.

First, let's note that you need to initialize the "bin" values to zero. But the real time saver for you would be to combine all the "bin" in a single array:

INTEGER bin(20)
INTEGER i,a
REAL x


DO i=1,20
bin(i)=0
ENDDO

then it is a matter of doing your count in this way

DO a=1,20000
x=rand(a)
i=IFIX(x*20.)+1
C note: that is the clever part; the value of "x" is
C converted to an index into "bin", no testing is required
C The IFIX function converts the real value into a floor
C integer, hence 19.9999 is truncated to 19, which is why
C the value is incremented by one
bin(i)=bin(i)+1
ENDDO


Note the massive saving in the humber of lines of codes, and a much more streamlined and efficient operation, since you avoid all those IF ELSE tests (which should average 10 tests for each iteration).


Then to print those all:

WRITE(*,*) (bin(i),i=1,20)


Note that, in your original program, you have the write statement inside the loop. If you don't really care to get 20000 lines as output, perhaps it should not be there...


CBVG
 
Thank you so much for your help. I got it to work using my strategy, but yours is a lot simpler and more efficient.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top