• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: 2d array and random number generator
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 2d array and random number generator


  • Subject: Re: 2d array and random number generator
  • From: Rhys Hill <email@hidden>
  • Date: Mon, 26 Sep 2005 06:51:57 +0930

I've thought about this exact problem before!

I think the trick is to start with a correct solution, then apply lots of
permutations to it, making it random. Say it was just 1-4 instead of 1-9,
you might start with:


1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3

then you perform row and column swaps until it's random enough. ie.

for ( int i = 0; i < 10000; i++ )
{
    int src = 4 * random() / (double) RAND_MAX;
    int dst = 4 * random() / (double) RAND_MAX;
    if ( random() / (double) RAND_MAX < 0.5 )
    { /* Row swap */
        ...
    }
    else
    { /* Column swap */
        ...
    }
}

The problem with your original code is that the chances of coming up with
a valid solution are extremely low, whereas this way your solution is
always correct, just not necessarily mixed up enough.


BTW, this is majorly off-topic!

Cheers,

On 26/09/2005, at 4:42 AM, email@hidden wrote:

Hi, I'm trying to build a SuDoku 9x9 grid of random numbers with 9 3x3
sub grids, and obviously the numbers 1 - 9 should not repeat in certain
parts of the grid. I chose to use a 2d array containing 81 random
integers, and then made a function that checked each part of the array
to correct numbers that had repeated where they shouldn't.


here is an example of part of the check function:

do
        chk[1][4]=rand()%9+1;

while(chk[1][4]==chk[0][3]||chk[1][4]==chk[0][4]||chk[1][4]==chk[0] [5]||
chk[1][4]==chk[1][0]||chk[1][4]==chk[1][1]||chk[1][4]==chk [1][2]||
chk[1][4]==chk[1][3]);


I know this must look very crude, but in actual fact it does work, it's
just that it seems to get stuck in an infinite loop sometimes when ran.
Is this because of using too many 'or' operators.
Don't be too critical, I am pretty new at all this, and there is
probably easier an way.


Cheers,
Carl.

p.s. Layman's terms please!

------------------------------------------------------------------------ --------
Rhys Hill, email@hidden
Hons. Maths & Comp. Sci


Phone: +61 8 8303 6197 Mail:
Fax: +61 8 8303 4366 School of Computer Science
University of Adelaide
Adelaide, Australia
http://www.cs.adelaide.edu.au/~rhys/ 5005
------------------------------------------------------------------------ --------




_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Overriding the C++ Operator new
  • Next by Date: Jaguar: dyld: can't open /usr/lib/libmx/A.dylib
  • Previous by thread: 2d array and random number generator
  • Next by thread: Overriding the C++ Operator new
  • Index(es):
    • Date
    • Thread