fill array with random number

This is a discussion on fill array with random number within the C++ Programming forums, part of the General Programming Boards category; Hi, I've been trying to fill an 6x6 array with random number from 1 to 6, this is my function ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    43

    fill array with random number

    Hi,
    I've been trying to fill an 6x6 array with random number from 1 to 6,
    this is my function that takes care of this
    Code:
    void initArray(int arrayName[][col], int row, int col)
    {
    	
    	for (int i =0; i<row; i++)
    	{
    		srand(static_cast<unsigned int>(time(NULL)));
    		for (int j=0; j<col; j++)
    		{	
    			arrayName[row][col]= 1+rand()%6;
    		}
    	}
    
    }
    but, when i print out the array, its elements are all the same ...
    am I missing anything? (i cannot find anything that is wrong)

    thanks for any insight

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Do not initialize the seed more than once.
    Call srand at the start of your app and nevermore. Then just call rand to assign and you'll see you'll get random numbers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    43
    I had tried that, but the output elements still remains the same.
    It should be different for each of the element; however, in this case, they're the same.

    am I missing anything?
    thanks!

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Well, you are assigning all your random values to the same array item: arrayName[row][col] (which probably is out of bounds anyway).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    43
    i though the nested for-loop will loop through each element?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I can't get your function to compile, since the second array dimension isn't given. Also you never assign to valid locations (array[row][col] isn't valid -- it's off both edges of the array -- you should be writing to array[i][j] instead).

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    43
    Ahh! I see. I totally forgot about that..
    row=col=6 .. it's a 6x6 array and i tried to generate random number from 1 to 6 with 1 + rand() &#37;6

  8. #8
    Registered User
    Join Date
    May 2008
    Posts
    43
    1+rand()&#37;6 should give out number from 1 to 6 right?
    in my case, i got all weird number like -123655390

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> 1+rand()&#37;6 should give out number from 1 to 6 right?
    Yes. (Or maybe 1+(rand()%6), I don't remember the precedence.)

    Did you fix the arrayName[row][col]= bug?

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You iterating indices are i and j. You must use those for indexing the array, not the array bounds row and col.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    May 2008
    Posts
    43
    hi guys
    my bad...how come i didn't see that...
    it's working..it's my printArray function that prints out out-of-bound element...

    thanks again!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number generation without repeats
    By kishore84 in forum C Programming
    Replies: 1
    Last Post: 02-05-2009, 11:17 AM
  2. Issue w/ Guess My Number Program
    By mkylman in forum C++ Programming
    Replies: 5
    Last Post: 08-23-2007, 01:31 AM
  3. random fill array
    By zeromx in forum C Programming
    Replies: 7
    Last Post: 11-07-2006, 05:18 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21