0

I need an 2d array [9,16] with 144 numbers from 1->36 in random order (so each number is repeated 4 times).

1
  • It sounds more like you want a shuffle algorithm. The numbers themselves are not random at all - just their order. I suggest editing the question to clarify that. Commented Jan 9, 2010 at 4:45

3 Answers 3

4

Something like:

sourcearr = array();
for(i = 0; i < 36; i++){
  for(j = 0; j < 4; j++){
      sourcearr[i+j] = i;
   }
}

sourcearr = shuffle(sourcearr)

k = 0;
myrandarr = array();
for(i = 0; i < 9; i++){
    myrandarr[i] = array();
    for(j = 0; j < 16; j++){
        myrandarr[i][j] = sourcearr[k++];
    }
}

where you use shuffle.

Sign up to request clarification or add additional context in comments.

2 Comments

I forgot to say that each number is repeated 4 times, it isn't just random.
So there is nothing such as: Enumerable.Range(1,36)?
2

Assuming you don't care about the distribution, just store the results of

Math.floor(Math.random()*36) + 1

for each element of the array

1 Comment

This won't repeat each number 4 times however.
0

How's about:

var source = array();
var shuffled = array();

for(var i=0;i<4;i++)
{
  for(var j=0; j<36;j++)
  {
    source[i*j] = j+1;
  }
}

while( source.length > 0 )
{
  var index = Math.floor(Math.random()*source.length);
  var element = source.splice(index,1);
  shuffled.push(element);
}

Comments

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.