hello everyone, i'm trying to make a program that looks like a monte carlo method for predicting the internal values of a matrix by stochastic means (through random walks)
the program works by setting a start point (each element of the matrix at a time) and then walking pseudorandomly throughout the matrix until it hits a border (and try to move out of it). once it does, it takes a user preset value from that border, sum up to a variable and then start over another path beginning from the sought element again. this goes on until it has hit ’n’ times whatever border.
by the end it divides the summed values by ’n’, and in theory should give an approximate value.
then it does the same for the next element.
i am trying to solve it for a 3x3 matrix (with 83 and -83 vertical borders values, and 21 and -21 horizontal ones). i’ve already did it by other means like ‘jacobi’s method’ and the ‘discret mean value’, and i have fairly accurate values for these elements.
what happens is that once i run the program only the first element of the matrix seems to be coherent (slightly out synced i might say). the others are a total mess.
i don’t know if the problem is on the algorithm i’ve built, or if it’s some writing gap.
i’m learning to programming and it’s probably a silly issue, but i can’t seem to find what might be causing this flaw in order to look for a solution.
if anyone is interested in helping me i’ll be glad
the code is here #1852027 - Pastie
2 replies to this topic
#1
Posted 30 April 2011 - 04:29 PM
|
|
|
#2
Posted 01 May 2011 - 10:10 AM
This line
Notice what is says about maxValue, that it is the exclusive upper bound. So your generator only gets values from 1 to 3.
Another thing is it isn't a good practice to declare a Random instance inside a loop. If the loop runs fast enough, you will get the same generator over and over. Declare it as a class variable so you only ever generate one Random instance.
Don't know if this is your problem, but it is a problem (and something a lot of people don't realize).
a = r.Next(1, 4);doesn't do what you think it does. Read the documentation carefully:
Quote
minValue
Type: System.Int32
The inclusive lower bound of the random number returned.
maxValue
Type: System.Int32
The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.
Type: System.Int32
The inclusive lower bound of the random number returned.
maxValue
Type: System.Int32
The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minValue.
Notice what is says about maxValue, that it is the exclusive upper bound. So your generator only gets values from 1 to 3.
Another thing is it isn't a good practice to declare a Random instance inside a loop. If the loop runs fast enough, you will get the same generator over and over. Declare it as a class variable so you only ever generate one Random instance.
Don't know if this is your problem, but it is a problem (and something a lot of people don't realize).
#3
Posted 01 May 2011 - 01:25 PM
dear Momerath,
thank you very much for your help, it worked like magic.
just replaced the maxValue to 5 and voila. :thumbup:
now i have a random walk calculator that works pretty acceptably.
i have also seen the problem door that might exist by keeping the random declaration inside the loop instance.
cheers
:)
thank you very much for your help, it worked like magic.
just replaced the maxValue to 5 and voila. :thumbup:
now i have a random walk calculator that works pretty acceptably.
i have also seen the problem door that might exist by keeping the random declaration inside the loop instance.
cheers
:)
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









