Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Selecting Random values

Received: by 10.180.93.6 with SMTP id cq6mr2116895wib.2.1352353504230;
        Wed, 07 Nov 2012 21:45:04 -0800 (PST)
From: "e p chandler" <e...@juno.com>
Newsgroups: comp.lang.fortran
Subject: Re: Selecting Random values
Date: Thu, 1 Nov 2012 18:00:57 -0400
Organization: A noiseless patient Spider
Lines: 116
Message-ID: <k6ureu$poo$1@dont-email.me>
References: <db1ea4de-313b-48c3-ab5f-2c43e0655443@googlegroups.com> <d4363b07-1b0f-4805-956a-05fc9d59138f@googlegroups.com> <31ab0564-18c9-445b-bde1-fa7953cb6e18@googlegroups.com> <k6snne$9gr$1@dont-email.me> <k6sur9$fgi$1@speranza.aioe.org> <835a4910-7c9c-4b19-90d5-10f9c91013bd@googlegroups.com>
Mime-Version: 1.0
Injection-Date: Thu, 1 Nov 2012 22:01:03 +0000 (UTC)
Injection-Info: mx04.eternal-september.org; posting-host="874585c0c3df857aab1c9a195f6ac5ec";
	logging-data="26392"; mail-complaints-to="ab...@eternal-september.org";	posting-account="U2FsdGVkX1/hjs9HDaaWIK2GXm4BPxKyZhVMdAJYFb4="
X-MimeOLE: Produced By Microsoft MimeOLE V15.4.3538.513
In-Reply-To: <835a4910-7c9c-4b19-90d5-10f9c91013bd@googlegroups.com>
X-Newsreader: Microsoft Windows Live Mail 15.4.3538.513
Importance: Normal
Cancel-Lock: sha1:Vu2icyEiJsk5LDwe3X9G4G3ZLxM=
X-Priority: 3
X-MSMail-Priority: Normal
Path: ha8ni167528wib.1!nntp.google.com!feeder2.cambriumusenet.nl!feed.tweaknews.nl!94.232.116.13.MISMATCH!feed.xsnews.nl!border-3.ams.xsnews.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newspeer1.nac.net!border4.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!news.mccarragher.com!news.grnet.gr!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail
X-Original-Lines: 7
Content-Type: text/plain;
	format=flowed;
	charset="iso-8859-1";
	reply-type=original
Content-Transfer-Encoding: 7bit

"Davk"  wrote
> > > For example, in first row of data (211110122122), I need to write a 
> > > program
> > > to read this row like this: 211 and write down in new file just one 
> > > "1"
> > > randomly, then 110 and write down in new file one "1", then, 122 and 
> > > etc.
> > > for all rows.

"Glen" replied
> > Now, what if there aren't any "1"s in the group?

"Davk" replied
> Ok, If there are not any "1" in each part, Fortran should be able to 
> ignore that part and go to the next.

Here is a small program that may do what you want. It also prints some 
intermediate results along the way.
Note that I have used character variables instead of integers, in particular 
an array of 4 "strings", each 3 characters long.

---- start ----
C:\Users\epc\temp>type x.f90
program split
implicit none
character(len=3) a(4) !4 fields of 3 characters each
integer b(4),m !list of fields that match, number of matches
integer ierr,i !i/o status, loop index
integer k !random position in the match list, if any
real u !random(0,1)

open(10,file='in.txt')
do
!read and echo input character data
  read(10,'(4a3)',iostat=ierr) a
  if (ierr /= 0) exit
  write(*,'(4(1x,a3))') a

!find matching fields
  m=0 !no matches yet
  b=0 !fill whole array for display purposes
  do i=1,4
    if (a(i) == '111') then
      m=m+1
      b(m)=i !add index of field to list
    end if
  end do
  write(*,'(4(1x,i1))') b !list of matches

!if any matches, generate a random integer 1..m
  if (m == 0) then !list empty
    write(*,'(a)') 'no matches'
  else
    call random_number(u)
    k=int(1+m*u) ! 1..m !random position in the list
    write(*,'(a,i1,a)') 'list item ',k,' is drawn'
    write(*,'(a,a3)') 'and the winner is ',a(b(k))
  end if
end do
end program split

C:\Users\epc\temp>g95 x.f90

C:\Users\epc\temp>a
211 110 122 122
0 0 0 0
no matches
121 011 111 110
3 0 0 0
list item 1 is drawn
and the winner is 111
121 112 111 211
3 0 0 0
list item 1 is drawn
and the winner is 111
211 121 111 111
3 4 0 0
list item 1 is drawn
and the winner is 111
100 122 221 121
0 0 0 0
no matches
121 101 211 211
0 0 0 0
no matches
121 121 111 112
3 0 0 0
list item 1 is drawn
and the winner is 111
111 200 112 111
1 4 0 0
list item 2 is drawn
and the winner is 111
112 111 110 111
2 4 0 0
list item 2 is drawn
and the winner is 111
111 211 111 112
1 3 0 0
list item 1 is drawn
and the winner is 111

C:\Users\epc\temp>
---- end ----

Note that m is the number of matches. Note that the B array contains the 
numbers from 1..4 of any fields that match.
Now there may be some constructs in modern Fortran that make this easier, 
but this "old school" aproach works.
Of course , all the "winners" are "111". How boring [smile]. Also this 
illustrates some Fortran idioms for you.

--- e