[R] 2D random walk with traps convert C++ code to R code

From: Amanda Zeqiri <amandazeqiri_at_hotmail.com>
Date: Mon, 30 May 2011 23:21:48 +0200

Hello, I have a C++ code for 2D random walks with traps and I want to convert it in a R code with its syntaxs, can anyone help??????? It's easy for me to adapt the body but I want help with the beginig (variable declaration) and th end exporting the output to a file ( like write.table() or sink() )

Thank you...

#include <iostream>
#include <math.h>#include <fstream.h>#include <time.h>
#define IA 16807#define IM 2147483647#define AM (1.0/IM)#define IQ 127773
#define IR 2836#define NTAB 32#define NDIV (1+(IM-1)/NTAB)#define EPS 1.2*pow(10,-7)#define RNMX (1.0-EPS)
using namespace std;float rand1(long *idum);

int main(){ long run=10000,adr=-run,x,y,hapa=0,i=0; int dimx=700,dimy=700,nrg=50, M[dimx][dimy]; double rnd,sum=0; long k[run+1],frekuenca;  bool njehsuar[run+1], next; ofstream Txt("EndjeMePengesa2D.txt"); printf("\nProgrami ekzekutohet:\ni=1 ne %d",run); for (i=1;i<=run;i++){ njehsuar[i]=true; k[i]=0; } while (i<nrg){ x=dimx*rand(&adr);y=dimy*rand(&adr); if (M[x][y]!=1){ M[x][y]=1;i++
 } }for (i=1;i<=run;i++){x=dimx*rand1(&adr); y=dimy*rand1(&adr);  hapa=0; next=false; if (M[x][y]!=1){while (!next){hapa+=1;rnd=rand1(&adr);if (rnd<=0.25){ x+=1; if (x>dimx) {x=0;}}else if (rnd >0.25 && rnd<=0.5){x-=1;  if (x<0) {x=dimx;}}else if (rnd >0.5 && rnd < 0.75){ y+=1; if (y>dimy) {y=0;} }else{ y-=1; if (y<0) {y=dimy;}}if (M[x][y]==1) {next=true;} } } k[i]=hapa; if (i%1000==0) printf("\ni=%d",i); } Txt.close();

 cout << "-------------------------------------------"<< endl;
 cout << "Hap skedarin EndjeMePengesa2D.txt per te pare rezultatet." << endl; system("PAUSE"); return 0;}                                                

        [[alternative HTML version deleted]]

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Mon 30 May 2011 - 21:46:46 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 30 May 2011 - 21:50:11 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive