0

this is my first post:) Alright I'm trying to create a program that randomly either adds 1 or -1 to the start value of 0. This process proceeds in 500 steps or so and then the end result needs to be added to an array. This is how far I've reached:

using System;
using System.Collections.Generic;
namespace dimensionrandomwalk
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            Random rnd = new Random();
            int[] x;
            x = new int[500];
            for (int i = 0; i < 500; i++){
                int L = rnd.Next (0, 2);
                int L1 = -1;
                int L2 = 1;
            if (L == 0) {
                    x [i] = L1;
            } else if (L == 1) {
                x [i] = L2;
            }
                x[i] = x[i+1]+x[i];

                Console.WriteLine (x[i]);
                }


        }
    }
}

I'm going to make two arrays, one that contains the 500 steps and one with 100 end results. but I can't get it to add x[1] with x[2] and so on untill x[500]. Thank you:)

5
  • Oh just quickly, the last console.writeLine is simply to give the output so I can see if it worked, but it hasn't so far:/ Commented Nov 8, 2013 at 8:19
  • Do you want to add 1 or -1 to the same value 500 times or to 500 different values? Commented Nov 8, 2013 at 8:20
  • Are you interested in keeping all the arrays of 500 steps afterwards, or is the required output just the 100 end results? Commented Nov 8, 2013 at 8:21
  • 1
    You speak of two arrays, yet I see only 1 in your question. Try renaming your variables so that your stuff is more readable. Commented Nov 8, 2013 at 8:21
  • @tahtmat & Baldrick: The start value is 0, then 500 times it has to randomly +1 or -1. Like 0-1+1+1+1-1-1+1-1+1+1-1... = x and x has to be added to an array that I'll create later Mithon: I've not come that far yet, but I was just telling my future plan for the program. Commented Nov 8, 2013 at 8:26

2 Answers 2

1

Think this is what you want:

using System;
using System.Collections.Generic;
namespace dimensionrandomwalk
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            Random rnd = new Random();
            int[] x;
            x = new int[500];
            for (int i = 0; i < 500; i++){
                int L = rnd.Next (0, 2);

                x[i] = (L==0) ? -1 : 1;

                Console.WriteLine (x[i]);
            }

            int total_value = 0;
            for (int i = 0; i < 500; i++
                total_value += x[i];

            Console.WriteLine ("Total: " + total_value);
        }
    }
}

Or if you don't care about the temp array:

using System;
using System.Collections.Generic;
namespace dimensionrandomwalk
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            Random rnd = new Random();

            int total_value = 0;

            for (int i = 0; i < 500; i++){
                int L = rnd.Next (0, 2);

                total_value += (L==0) ? -1 : 1;            
            }

            Console.WriteLine ("Total: " + total_value);
        }
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

I just ran the code, it said Total: 20, is that the end result of all the 0+1-1+1... ?
yep. ... i guess every time you'll see something else ... but it should be around 0 anyway, since it's like a coin flip ...
@ Noctis I think this might be exactly what I needed. and it runs 500 times?
Super thanks, now I need to make a second array the adds the end value (the total_value) in 100 slots and then tell me how many times it landed on e.g -20 to 20
Will be very similar to the above code ... I'm sure you can handle it from here :)
|
1

You can use LINQ to get what you want. This code generates 100 random walks with 500 iterations of +1/-1 each:

var rand = new Random();
var walks = Enumerable.Range(1,100)
                      .Select(item => Enumerable.Range(1, 500)
                      .Select(i => rand.Next(0, 2) * 2 - 1)
                      .Aggregate((i, j) => i + j));

Each item in the 'walks' IEnumerable contains the result of a 500 iteration random walk.

3 Comments

I would have gone with something like this, but my guess is it might be a bit over his head :)
Fair point.. Might be useful to someone else searching for random walk questions though.
Absolutely good ser ... absolutely ... here's an upvote for you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.