PDA

View Full Version : Random Numbers Help Required


mashar786
08-17-2009, 08:22 AM
Hi

I want to create a random matrix say of size 3x3. By the help of ran
command i am getting a matrix like

A=[ 1.xxxx 2.xxxx 3.xxxx ; 4.xxxx 5.xxxx and so on]

But the thing i required is the matrix of size 3x3 randomly created suc
that it should have all the elements from 1 to 9.

I tried round(rand(rows,columns)), but here problem i am getting is tha
some numbers are repeated also eg 3.2323 and 3.9494 both are rounded to 4
which i dont require. My requirement is to recieve unique whole numbe
matrix consisting of elements 1 to m*n.

Rune Allnor
08-17-2009, 12:13 PM
On 17 Aug, 09:22, "mashar786" <[email protected]> wrote:
> Hi
>
> I want to create a random matrix say of size 3x3. By the help of rand
> command i am getting a matrix like
>
> A=[ 1.xxxx 2.xxxx 3.xxxx ; 4.xxxx 5.xxxx and so on]
>
> But the thing i required is the matrix of size 3x3 randomly created such
> that it should have all the elements from 1 to 9.
>
> I tried round(rand(rows,columns)), but here problem i am getting is that
> some numbers are repeated also eg 3.2323 and 3.9494 both are rounded to 4,
> which i dont require. My requirement is to recieve unique whole number
> matrix consisting of elements 1 to m*n.

Assuming you use matlab:

N = m*n;
A = 1:N;
for p=N:-1:3
q=ceil(rand*(p-1));
tmp = A(p);
A(p) = A(q);
A(q) = tmp;
end
A=reshape(A,m,n);

Rune

Jerry Avins
08-17-2009, 03:55 PM
mashar786 wrote:
> Hi
>
> I want to create a random matrix say of size 3x3. By the help of rand
> command i am getting a matrix like
>
> A=[ 1.xxxx 2.xxxx 3.xxxx ; 4.xxxx 5.xxxx and so on]
>
> But the thing i required is the matrix of size 3x3 randomly created such
> that it should have all the elements from 1 to 9.

That's not random, it's a shuffle. There's a lot of literature about that.

> I tried round(rand(rows,columns)), but here problem i am getting is that
> some numbers are repeated also eg 3.2323 and 3.9494 both are rounded to 4,
> which i dont require. My requirement is to recieve unique whole number
> matrix consisting of elements 1 to m*n.

Jerry
--
Engineering is the art of making what you want from things you can get.
ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ

Vladimir Vassilevsky
08-17-2009, 05:30 PM
mashar786 wrote:

> Hi
>
> I want to create a random matrix say of size 3x3. By the help of rand
> command i am getting a matrix like
>
> A=[ 1.xxxx 2.xxxx 3.xxxx ; 4.xxxx 5.xxxx and so on]
>
> But the thing i required is the matrix of size 3x3 randomly created such
> that it should have all the elements from 1 to 9.

#include <stdio.h>

unsigned int Factorial(unsigned int n)
{
unsigned int i,f = 1;

for(i = 2; i <= n; i++) f *= i;

return f;
}

//-----------------------------------------------
// Generate combination i from n! combinations
//
//
void GetCombination(unsigned int *c, unsigned int n, unsigned int i)
{
unsigned int j,k,l,m;

for(k = 0; k < n; k++) c[k] = n;

m = 0;
j = Factorial(n);


for(k = n; k >= 1; k--)
{
j /= k;
l = i/j;
i -= l*j;
l++;
do {
do {
if(++m == n) m = 0;
} while(c[m] != n);
} while(--l);
c[m] = k-1;
}
}

//--------------------------------------------
//
#define N 9

void main(void)
{
unsigned int i,j,Nfact;
unsigned int c[N];

Nfact = Factorial(N);

for(i=0;i<Nfact;i++)
{
GetCombination(c, N, i);

printf("\n");
for(j=0;j<N;j++) printf(" %u",c[j]);

}

printf("\n");
}

VR
08-17-2009, 11:13 PM
"mashar786" <[email protected]> wrote in message
news:[email protected] ...
> Hi
>
> I want to create a random matrix say of size 3x3. By the help of rand
> command i am getting a matrix like
>
> A=[ 1.xxxx 2.xxxx 3.xxxx ; 4.xxxx 5.xxxx and so on]
>
> But the thing i required is the matrix of size 3x3 randomly created such
> that it should have all the elements from 1 to 9.
>
> I tried round(rand(rows,columns)), but here problem i am getting is that
> some numbers are repeated also eg 3.2323 and 3.9494 both are rounded to 4,
> which i dont require. My requirement is to recieve unique whole number
> matrix consisting of elements 1 to m*n.

Jerry is right, your matrix of interest isn't random per se.
To form such a matrix by means of MATLAB, you may want to apply random
permutation function:

A = randperm(rows*columns)
B = reshape(A, rows,columns)

--Vladimir