Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7416361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:28:51+00:00 2026-05-29T07:28:51+00:00

A random walk is a stochastic process in which a particle moves one step

  • 0

A random walk is a stochastic process in which a particle moves one step at a time from state to state in a structured space. For us, the state space will be Z, the set of integers. The particle starts in an initial state S[0] ∈ Z. If, after i ≥ 0 steps, the particle is in state S[i], then in step i + 1, it moves to state S[i] + 1 with probability p and to state S[i] − 1 with probability q; it cannot stand still. Of course, p + q = 1. If S[0] = 5 and 0 < p < 1, then the sequence 5,4,3,4,3,2,3,2,3,4 is a possible sequence of states for the particle if it moves 9 times.

Write a program that will simulate a random walk for a given number of steps and that will compute certain statistics for the random walk. The parameters for a simulation come from standard input as a single line of parameters, consisting of (1) the initial state S[0]; (2) the value of p; and (3) the number of steps to simulate.

Note: I’m writing this in Java.
So far I have:

public static void main(String[] args) {

    Random rand = new Random();
    int iState = rand.nextInt();
    int particle = iState;
    double pValue = 0.60;
    int numSteps = rand.nextInt() + 1;
    int nSteps = 0;

    if (numSteps>=0) {
        System.out.println(particle);
        while (nSteps<numSteps); {

            if (rand.nextDouble() < pValue)
                particle++;

            else
                particle--;

            System.out.println(particle);
            nSteps++;

        }

    }

Something seems to be going wrong though, so I’m stuck.

EDIT: Thanks guys, somehow I didn’t catch that semicolon.

//—————————————————————————————-

EDIT 2: Okay, so I have the code working correctly; however, in the end I am supposed to listed the maximum, minimum, and average values. Is this possible to do without creating a new variable for each iState value? My new code is

 public static void main(String[] args) {

    Random rand = new Random();
    int iState = rand.nextInt();
    double pValue = 0.60;
    int numSteps = rand.nextInt(100) + 1;
    int nSteps = 0;

    if (numSteps>=0) {
        System.out.println(iState);
        while (nSteps<numSteps) {

            if (rand.nextDouble() < pValue)
                iState++;

            else
                iState--;

            System.out.println(iState);
            nSteps++;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:28:52+00:00Added an answer on May 29, 2026 at 7:28 am

    I think your problem is that you need to delete the semicolon in the line

    while (nSteps<numSteps); {
    

    Also, you almost certainly don’t want to iterate for rand.nextInt() + 1 steps, since rand.nextInt() could be negative. You might mean rand.nextInt(n) where n is some upper bound, though, or you might just set it to n.

    UPDATE: If you need to track the minimum, maximum, and average values, you might do something like

     int min = iState; // initial value
     int max = iState; // initial value
     long total = iState; // initial value
    

    Then, after each iteration, you say

     min = Math.min(min, iState);
     max = Math.max(max, iState);
     total += iState;
    

    and then at the end of the iteration, your minimum is in min, your maximum is in max, and your average is (double) total / numSteps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to solve a two-dimensional random walk problem from the book, exploring python.
I want to put random output from my result set (about 1.5 mil rows)
When using a Random Number Generator, which is the better way to use it
I have been running this random walk simulation for a while now and I
I've made a random walk simulation and I'd like it to output little circles
i have the following problem: Create a program where a particle will execute a
I have this problem : Create a program which constructs a lattice of one
from random import * N = 100 gamma = 0.7 connect = zeros((N,N)) for
I need to generate a random walk based on the DNA sequence of a
Math.random() in javascript is able to return 1, right? Which means if I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.