Thread: Random Numbers

  1. #16
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You will find that filling a Sudoku grid with random numbers is not going to cut it. Yes they're random, but they'll also repeat... violating the Sudoku rules.

  2. #17
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    I don't think so, because arrays start at 0.
    I've solved the problem of the large numbers with this:

    [code]
    if (sudoku[a][b]>9)
    {
    sudoku[a][b]=0;
    }
    [\code]

    But the dimensions of the produced grid is still fluctuating.

    ED: I know. I've noticed that. But I can fix that. This is my current code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    int check(int* x[8][8]);
    int main()
    {
        int sudoku[8][8] = {0};
        int i;
        int a;
        int b=0;
        int n;
        int m=0;
        int p;
        srand(time(NULL));
        for(a=0;a<9;a++)
        {
            p=0;
            for(;b<9;b++)
            {
                p=0;
                i=rand() %20;
                if (i<10 && i!=0)
                {
                    sudoku[a][b]=i;
                }
                if (sudoku[a][b]>9)
                {
                    sudoku[a][b]=0;
                }
                if (sudoku[a][b]!=0)
                {
                    for (n=a;n<9;n++)
                    {
                        if(n==a)
                        {continue;}
                        if(sudoku[a][b]==sudoku[n][b])
                        {
                            m=1;
                            break;
                        }
                    }
                    if (m==1)
                    {
                        b--;
                        p=1;
                    }
                    for (n=a;n>=0;n--)
                    {
                        if(n==a)
                        {continue;}
                        if(sudoku[a][b]==sudoku[n][b])
                        {
                            m=1;
                            break;
                        }
                    }
                    if (m==1 && p==0)
                    {
                        b--;
                        p=1;
                    }
                    for (n=b;n<9;n++)
                    {
                        if(n==b)
                        {continue;}
                        if(sudoku[a][b]==sudoku[a][n])
                        {
                            m=1;
                            break;
                        }
                    }
                    if (m==1 && p==0)
                    {
                        b--;
                        p=1;
                    }
                    for (n=b;n>=0;n--)
                    {
                        if(n==b)
                        {continue;}
                        if(sudoku[a][b]==sudoku[a][n])
                        {
                            m=1;
                            break;
                        }
                    }
                    if (m==1 && p==0)
                    {
                        b--;
                        p==1;
                    }
                }
                if (sudoku[a][b]==0 && p==0)
                {
                    printf("[ ]");
                }
                else
                {
                    if (p==0)
                    {
                        printf("[%d]", sudoku[a][b]);
                    }
                }
                p=0;
            }
            printf("\n");
        }
        getchar();
        return 0;
    }
    I am getting weird outputs, like [ ][ ][9] or [6][6]. Never more than one row.
    Last edited by mortalc; 03-12-2011 at 04:46 AM.

  3. #18
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    I've edited my last post to show my full code.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Are you trying to generate a Sudoku puzzle grid?

    That is harder than solving the puzzle, because:

    1) lots of grids that look OK originally, just can't be a valid puzzle grid. They lack the required characteristics to be a Sudoku puzzle.

    2) Real Sudoku fans don't consider puzzle grids with multiple solutions, to be valid.

    You can download tens of thousands of valid Puzzle grids, so generating your own grids is not efficient.

    A lot of posts were lost in a recent virus attack, but these guys & gals have forgotten more about Sudoku programming, than most people will ever learn:

    Sudoku Programmers :: Index
    Last edited by Adak; 03-12-2011 at 06:20 AM.

  5. #20
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    Hrm... Maybe you're right....

    How about this. Say I'm going for a solver. Basically this is my idea:

    1. Open Xcel, or another grid program (I think I can do this with fopen())
    2. User writes in the starting numbers into the first 81 squares (from A1 - I9).
    3. After a key press or something, the values are transferred to sudoku[9][9] (yes I realized my mistake)
    4. It is solved (I can do this bit).
    5. The solution is printed (I can do this) or opened as an Xcel or another grid program (preferable, not sure how to do this.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with displaying random numbers
    By Bumps in forum C++ Programming
    Replies: 12
    Last Post: 10-03-2009, 12:29 PM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  3. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  4. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  5. Generate random numbers in Lucky7 project using C#
    By Grayson_Peddie in forum C# Programming
    Replies: 1
    Last Post: 04-11-2003, 11:03 PM