tags:

views:

154

answers:

6

Thanks for all helped me before. But I still have some questions about the program. How to generate a new random number while the new random number is equal to the previous random number? Also how to transpose the matrix?

#include "stdafx.h" 
#include "stdlib.h"  
#include "time.h" 


int _tmain(int argc, _TCHAR* argv[]) 
{ 
    int num2 = 0; 
    int num=0, i, j;      
    int mtx[9][9] = {0};  

    while (num < 3 || num > 9) {  
        printf("Enter an integer (3-9): ");  
        scanf("%d", &num);  
    }  
do 
{ 
    srand(time(NULL));  

    switch (num) 
    { 
    case 3: num2 = rand() % 8; 
        break; 
    case 4: num2 = rand() % 15; 
        break; 
    case 5: num2 = rand() % 24; 
        break; 
    case 6: num2 = rand() % 35; 
        break; 
    case 7: num2 = rand() % 48; 
        break; 
    case 8: num2 = rand() % 63; 
        break; 
    case 9: num2 = rand() % 80; 
        break; 
    } 


    for (i=0; i < num; ++i)  
        for (j=0; j < num; ++j)  
            mtx[i][j] = num2;  
} 
while ( num2 == num2); 



    for (i=0; i < num; ++i) {  
    for (j=0; j < num; ++j)  
        printf("%i ", mtx[i][j]);  
    printf("\n");  
}  



    return 0; 
}


Update:

#include "stdafx.h"
#include "stdlib.h" 
#include "time.h"


int _tmain(int argc, _TCHAR* argv[])
{
    int prevNum2 = 0;
    int num2 = 0;
    int num = 0, i, j;     // Added initializers and loop counters 
    int mtx[9][9] = {0}; // Reserve enough space for the worst-case scenario 

    while (num < 3 || num > 9) { // Added input validation loop 
        printf("Enter an integer (3-9): "); 
        scanf("%d", &num); 
    } 

    srand(time(NULL)); 

    do{

    prevNum2 =num2;
    switch (num)
    {
    case 3: num2 = rand() % 8;
     break;
    case 4: num2 = rand() % 15;
     break;
    case 5: num2 = rand() % 24;
     break;
    case 6: num2 = rand() % 35;
     break;
    case 7: num2 = rand() % 48;
     break;
    case 8: num2 = rand() % 63;
     break;
    case 9: num2 = rand() % 80;
     break;

    }




    // Loop through the matrix elements we want, filling each with a random number     
    for (i=0; i < num; ++i) 
        for (j=0; j < num; ++j) 
            mtx[i][j] = num2; 
    }
    while (num2 == prevNum2);

    /* Do something with the matrix here (display it, etc) */ 

    for (i=0; i < num; ++i) { 
    for (j=0; j < num; ++j) 
        printf("%i ", mtx[i][j]); 
    printf("\n"); 
} 



    return 0;
}
+1  A: 

Like Neil said, srand() needs to be called once, at the beginning of the program. Every time you call srand you're seeding the random number generator, but with this program it'll always be seeded with the same time, so you'll end up with the same random number.

<joke>

Of course, that's perfectly random enough by some definitions:

XKCD

</joke>

Skilldrick
Oh...still not work after srand() out the loop.
Programme Newbie
What do you mean by not working?
Skilldrick
After I enter a number, still nothing print out.
Programme Newbie
See reiner's response?
Brian
Yes, but how to correct it to mean that "while ( new ranNum == previos ranNum); " ?
Programme Newbie
+1  A: 

I think you should change this line:

 while ( num2 == num2);

As this will go on indefinitely for any value of num2

EDIT:

to make my comment cleaerer here are the program bits to make it work:

do 
{ 
    srand(time(NULL));  

    switch (num)

becomes:

do
{
    int prevNum2 =num2;
    switch(num)

and

while ( num2 == num2);

becomes:

while( num2 == prevNum2);

FINAL EDIT:

in a response to your edit. This bit of code is clearly not what you intended:

for (i=0; i < num; ++i) 
    for (j=0; j < num; ++j) 
        mtx[i][j] = num2; 
}

What it does is fill the entire matrix with the same number.

what you actually want (probably) is that every next cell is filled with a different number. For this you need to have a loop construct of some sort.

Something along the lines of:

 for(int i; i<num*num; i++)
 {
      //draw random number bit (shortened for clearity... should be your whole switch bit)
      num2 = rand() % 8;

      mtx[i%num][i/num] = num2;
 }

thats it... This way the entire matrix gets filled with random values. No need to check if the value was already drawn before...since that was not the problem

Toad
I think this statment is wrong too, but how could I correct it?I want to mean that "while ( new ranNum == previos ranNum); "
Programme Newbie
do it exactly as you just typed. Before the switch type: int prevNum2 = num2; ... and the while becomes: while(num2==prevNum2)
Toad
as a sidenote: Having such a condition beats the purpose of the rand(). It's perfectly possible to have 2 random numbers being the same one after the other(especially using such a small numeric range). With your condition you are effectively crippling the random distribution.
Toad
I understand what you say, but how to code it?I have tried several times but still syntax error.
Programme Newbie
newbie: see changed answer.... For specific syntax errors it helps if you post them, so we know too what the problem is ;^)
Toad
Thanks, no error now. But still cannot generate new random numbers diffient with pervious numbers. I have updated codes above.
Programme Newbie
newbie: see revised (yet again) answer. I finally understand your whole point ;^)
Toad
Yes, I want to fill diffient numbers in the matrix.For example, if user enter "3", the matrix is 3 X 3, and numbers between 0 - 8 are filled in the matrix randomly.If user enter "5", the matrix is 5 X 5, and numbers between 0 - 24 are filled in the matrix randomly.Anyway, thanks for your help:)
Programme Newbie
@newbie: don't forget to +1 and accept the answer if this now works.
Toad
@reinier: Of course! But I need to leave now, I will try later, thanks.
Programme Newbie
A: 

do { .. } (x == x)

is an infinite loop. You don't have any break statements to exit this.

Pod
+2  A: 

The code section which reads

 switch (num)
   {
   case 3: num2 = rand() % 8;  break;
   case 4: num2 = rand() % 15; break;
   case 5: num2 = rand() % 24; break;
   case 6: num2 = rand() % 35; break;
   case 7: num2 = rand() % 48; break;
   case 8: num2 = rand() % 63; break;
   case 9: num2 = rand() % 80; break;
   }

could be rewritten as

 num2 = rand() % ((num * num) - 1);

which is more compact and arguably clearer.

Bob Jarvis
A: 

I think you're trying to initialize elements in mtx with unique random integers from 0 to n-1, where n is the number of elements in mtx (does mtx represent a sudoku cell?). If this is the case:

Your for loop sets all the elements of mtx to the same value. So, if you are trying to do what I think, that wouldn't work in any case.

What you want to do is to shuffle a list of numbers from 0 to n-1. Fisher-Yates shuffle is pretty simple and efficient.

If you don't want to deal with malloc and friends, here's one way to do it.

 /* maximum possible size, replace ... with actual numbers */
int list[] = {0,1,2,3,4,5,6,7,8,...,80};

/* function to shuffle the first n elements of list */
void shuffle(int *list, size_t n);

and then, when you know the value of num.

shuffle(list, num*num);
for (i=0; i < num; ++i)
    for (j=0; j < num; ++j)
        mtx[i][j] = list[i*num+j];

and you can get rid of all code dealing with random numbers, your do...while loop, etc.

I haven't written shuffle() for you, becuase I think it will be a fun exercise for you to do. :-)

Finally, your prompt:

printf("Enter an integer (3-9): ");

might not be displayed to the user at the time you call printf() above, because stdout is line buffered by default on many systems. To make sure it's displayed before your program waits for input, do:

fflush(stdout);

before you try to read the input. This will make sure the prompt gets displayed properly.

Alok
A: 

I believe that the easiest way to fix this without radical changes is to eliminate the do-while loop. If you want to generate a matrix (or do anything to each element of a matrix - display, modify, etc...), the simplest approach is to work inside of a double nested for-loop. Your variable 'prevnum2' isn't really needed.

for (i = 0; i < num; i++) {
  for (j = 0; j < num; j++) {
    switch (num) {  // generate a random number
      ...
    }

    mtx[i][j] = num2;  // store the random number in the matrix
  }
}

When you move onward and upward in your programming life, you'll definitely want to look into Alok's excellent suggestions for better random number generation techniques.

RD