Last active
March 15, 2023 23:46
-
-
Save rintukutum/440b6b9b1491a46e9308cdb2568ae351 to your computer and use it in GitHub Desktop.
random walk tends to right
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(R6) | |
| library(animation) | |
| Walker2 <- R6Class( | |
| classname = 'Walker2', | |
| public = list( | |
| x = NULL, | |
| y = NULL, | |
| col = NULL, | |
| cex = NULL, | |
| initialize = function( | |
| x = NA, | |
| y = NA, | |
| col = NA, | |
| cex = NA | |
| ) | |
| { | |
| self$x <- x | |
| self$y <- y | |
| self$col <- col | |
| self$cex <- cex | |
| }, | |
| # all functions below | |
| #----- | |
| # update x and y | |
| step = function(){ | |
| # sx <- sample(c(-0.1,0,0.1),1) | |
| # sy <- sample(c(-0.1,0,0.1),1) | |
| probs <- runif(1) | |
| if(probs < 0.4){ | |
| self$x <- self$x + 0.1 | |
| } else if(probs < 0.6){ | |
| self$x <- self$x - 0.1 | |
| } else if(probs < 0.8){ | |
| self$y <- self$y + 0.1 | |
| } else { | |
| self$y <- self$y - 0.1 | |
| } | |
| }, | |
| #----- | |
| # display the content | |
| display = function(){ | |
| points(self$x, self$y, pch=19, col = self$col, cex=self$cex) | |
| }, | |
| changeCol = function(){ | |
| alpha <- paste0( | |
| sample(c(c(0,3,7,9), | |
| LETTERS[1:5]),1), | |
| sample(c(c(0,3,7,9), | |
| LETTERS[1:5]),1) | |
| ) | |
| if(nchar(self$col) == 9){ | |
| self$col <- tolower(paste0( | |
| substr(self$col,1,7), | |
| alpha)) | |
| }else{ | |
| self$col <- tolower(paste0(self$col, | |
| alpha)) | |
| } | |
| }, | |
| new.cex = function(){ | |
| self$cex <- runif(1,min = 0.01,max = 0.6) | |
| }, | |
| #---- | |
| # check if x and y values are out of bound | |
| reset = function(){ | |
| if(self$x >= 4){ | |
| self$x <- self$x - 0.1 | |
| } | |
| if(self$x <= -4){ | |
| self$x <- self$x + 0.1 | |
| } | |
| if(self$y >= 4){ | |
| self$y <- self$y - 0.1 | |
| } | |
| if(self$y <= -4){ | |
| self$y <- self$y + 0.1 | |
| } | |
| } | |
| ) | |
| ) | |
| run <- function(w){ | |
| w$step() | |
| w$reset() | |
| w$changeCol() | |
| w$new.cex() | |
| w$display() | |
| return(w) | |
| } | |
| many.walk <- function(n, cols){ | |
| ys <- runif(n=n,min = -3.8,max =3.8) | |
| xs <- runif(n=n,min = -3.8,max =0) | |
| ws <- list() | |
| for(i in seq_along(1:n)){ | |
| ws[[i]] <- Walker2$new(x=xs[i],y=ys[i], col=cols[i], cex = 0.2) | |
| } | |
| return(ws) | |
| } | |
| random.walk3 <- function(balls=20, iter=100,seed=1043, record=FALSE){ | |
| set.seed(seed) | |
| if(record == TRUE){ | |
| ani.options(ani.width=400,ani.height=400) | |
| ani.record(reset = TRUE) | |
| par(bg = "white") | |
| } | |
| plot(x=0,y=0, | |
| xlab = 'x', | |
| ylab = 'y', | |
| xlim = c(-5,5), | |
| ylim = c(-5,5), | |
| type = 'n', | |
| main = 'random-walk-tends-2-right') | |
| cols <- sample(x = RColorBrewer::brewer.pal(n = 9,'Paired')[-(1:2)],size = balls,replace = TRUE) | |
| balls <- many.walk(n = balls, cols=cols) | |
| for(i in seq_along(1:iter)){ | |
| balls <- lapply(balls,run) | |
| if(record == TRUE){ | |
| ani.record() | |
| } | |
| } | |
| } | |
| random.walk3(iter = 300,record=TRUE) | |
| #oopts = ani.options(interval = 0.1) | |
| #animation::saveGIF(ani.replay(), | |
| # movie.name = 'random-walk-right-tends-2-right.gif' | |
| # ) | |
| #dev.copy2pdf(file="~/Desktop/random-walk.pdf") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment