Message Boards Message Boards

0
|
2575 Views
|
5 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Unintended behaviour of a Do cycle and RandomInteger?

Posted 2 years ago

I am trying to generate a random matrix with sum of entries to be equal to 2048. I am generating it by adding 1 to a random element in the matrix given number of times. I am using the following code

X = Table[0, {i, 1, 32}, {j, 1, 32}];

Do[X[[RandomInteger[{1, 32}], RandomInteger[{1, 32}]]]++, {k, 1, 2048}]

However when I sum all entries after a run of a Do cycle I do not obtain 2048. I usually obtain something close to it. What is worse, when I try to increase a value of a single element at the start it often gets erased and it disappears from the matrix. The code I use for this is

X = Table[0, {i, 1, 32}, {j, 1, 32}];

X[[16, 16]]=300;

Do[X[[RandomInteger[{1, 32}], RandomInteger[{1, 32}]]]++, {k, 1, 1748}]

There are some rare instances when 300 remains in the matrix. That corresponds to the case when Do cycle did not try to add 1 into a given element. What am I missing? What is the problem within this code?

POSTED BY: Michal Jex
5 Replies

Perhaps you feel more comfortable with Sum?

Depending on your code you might need Normal to get rid of SparseArray.

Sum[SparseArray[RandomInteger[{1, 32}, 2] -> 1, {32, 32}], {2048}]

Normal[Sum[SparseArray[RandomInteger[{1, 32}, 2] -> 1, {32, 32}], {2048}]]

Robert

POSTED BY: Robert Nowak
Posted 2 years ago

Hello Robert,

your solution works fine. I just never used //Total before, so I am not certain, where it can be placed within the remainder of my code.

MJ

POSTED BY: Michal Jex

Hi Michal

Whats "wrong" with my suggested solution?

Robert

POSTED BY: Robert Nowak

Hi Michal

You code relies on side effects. Think of it expanding to the following:

X = Table[0, {i, 1, 32}, {j, 1, 32}];
Do[
     X[[RandomInteger[{1, 32}], RandomInteger[{1, 32}]]] = 
      X[[RandomInteger[{1, 32}], RandomInteger[{1, 32}]]] + 1, {k, 1, 
      2048}]

As you can see the source and the target address do not match any more. In general side effect code should be avoided in any programming language.

On solution could look like this:

Table[SparseArray[RandomInteger[{1, 32}, 2] -> 1, {32, 32}], 
  2048] // Total

Robert

POSTED BY: Robert Nowak
Posted 2 years ago

Hello Robert,

thank you for the reply. I did not realize that in the expression

X[[RandomInteger[{1, 32}], RandomInteger[{1, 32}]]]++

Mathematica calls RandomInteger four times instead of two times. Adding internal variable into the expression seems to fix the issue.

Do[{aa, bb} = {RandomInteger[{1, 32}], RandomInteger[{1, 32}]};  X[[aa, bb]]++, {k, 1, 1748}]

Is there a way to avoid introducing {aa,bb}. It seems to me that the functions are evaluated in different order than I intended them to.

POSTED BY: Michal Jex
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract