# performance – ASCII Random Walk Algorithm I would like some advice on improving/optimizing a small program I wrote. What it does is run a random walk on a torus manifold with 8 colors that loop around.

``````//RTM Test Using Random Walk Fractals, WIP
//Created by delta23
//This Code works on Minix, Unix, MacOSX, Linux
//This Code does not work on DOS or Windows (yet...)
//LICENSE OF CODE: CC0 - Public Domain

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define TAPE_SIZE_X 672
#define TAPE_SIZE_Y 135
//Q!><^V became ><^V : move and add
char inst ( 4 ) = { '>', '<', '^', 'V' } ; //can be used for debugging, and for inputting code later on, when it starts in a conditional loop perhaps
unsigned char tape ( TAPE_SIZE_X ) ( TAPE_SIZE_Y ) ;
int x = 336 ;
int y = 67 ;

void main ( void ) {
system ( "clear" ) ; //clear screen
srand ( time ( NULL ) ) ; //initialize random generator; TODO: port to C++ and use TRNG instead
printf ( "e(?25l" ) ; //disable cursor
//TODO: Implement program input and conditional commands
while ( 1 ) {
switch ( inst ( rand ( ) % 4 ) ) { //MOVE
case '>' :
x = x + 1 ;
break ;
case '<' :
x = x - 1 ;
break ;
case '^' :
y = y + 1 ;
break ;
case 'V' :
y = y - 1 ;
break ;
}
if ( x >= TAPE_SIZE_X ) x = 0 ; //TOROID CODES FOR OOB CURSOR
else if ( x < 0 ) x = TAPE_SIZE_X - 1 ;
if ( y >= TAPE_SIZE_Y ) y = 0 ;
else if ( y < 0 ) y = TAPE_SIZE_Y - 1 ;
tape ( x ) ( y ) = tape ( x ) ( y ) + 1 ; // ADD 1
if ( tape ( x ) ( y ) == 8 ) tape ( x ) ( y ) = 0 ;
usleep ( 16667 ) ; // CAP SPEED TO 60FPS : 1/60 seconds = 16666.666... microseconds
printf ( "33(%d;%dH", y, x ) ; //print @ cursor position
switch ( tape ( x ) ( y ) ) { //Color Code for Mem
case 0 :
printf ( "x1B(40m " ) ;
break ;
case 1 :
printf ( "x1B(41m " ) ;
break ;
case 2 :
printf ( "x1B(43m " ) ;
break ;
case 3 :
printf ( "x1B(42m " ) ;
break ;
case 4 :
printf ( "x1B(46m " ) ;
break ;
case 5 :
printf ( "x1B(44m " ) ;
break ;
case 6 :
printf ( "x1B(45m " ) ;
break ;
case 7 :
printf ( "x1B(47m " ) ;
break ;
}
fflush ( stdout ) ; //flush stdout to prevent abnormal lag in printing to screen
}
}
$$```$$
`````` Posted on Categories Articles