1. Write a function xrand(x) which takes as input a number from 0 to 100 and as output returns 1 with likelihood x% and returns -1 with likelihood (100 - x)%. For example, if you evaluate wrand(70.8) one hundred thousand times, you should see approximately 70,800 1s and 29,200 -1s. (Make sure your function works even if w is not an integer.)
2. Write a “weighted” random walk rwalk(n,x) which continues until it is a distance of n from the origin and where the steps are taken using the wrand(x) function. (You should not be typing any of the same code from wrand in this function. Instead you should just be typing “wrand”, which will use the function you’ve just written.)

댓글 수: 3

James Tursa
James Tursa 2018년 2월 8일
What have you done so far? What specific problems are you having with your code?
this is what i have done so far, but it only work for integer and only always gives me -1 for whatever value i input.
function []=wrand(w)
for i = 0:100
if w==rand(w)
w=1;
else w==(100-w);
w=-1;
end
end
disp(w)
end
Jos (10584)
Jos (10584) 2018년 2월 8일
Think about:
  • what does the statement rand(w) return
  • when does w==rand(w) evaluate to true?
  • how many statements are there in the else-block?

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2018년 2월 9일

0 개 추천

I have a few random walk demos. You might look them over. Adapt as needed if they look close to what you want.

댓글 수: 2

Natesha Alexander
Natesha Alexander 2018년 11월 18일
I am trying to simulate a symmetric random walk on a line where deltax and delta t =0.1
Image Analyst
Image Analyst 2018년 11월 18일
OK, good luck.
If you ever do have a question, post in a new link with your code.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Random Number Generation에 대해 자세히 알아보기

질문:

2018년 2월 8일

댓글:

2018년 11월 18일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by