Thread: Random Numbers

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    2

    Random Numbers

    The program I have is as follows:
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #define SIZE 4
    
    int main()
    {
        int a=0,b=0;
        int x[SIZE];
        int y[SIZE];
        
        srand(time (NULL));
        for( a=0; a<SIZE; a++ )
        {
             x[a] = 1 + (rand() %10);
             for( b=0; b<SIZE; b++ )
             {
                  y[b] = 1 + (rand() %10); 
                  printf( "[%d][%d] ", x[a], y[b] );
                  } 
             printf( "\n" );
             
        }
        scanf("%d");
        return 0;  
    }
    I am trying to get both arrays to return random numbers, but the only numbers that appear truly random are those of the second array. I don't understand why this is, as they are both programmed the same. Can anyone point me to my error and how to correct it?

    Thank you very much for your time,
    Brent

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you print out a lot more x[a] without ever changing a

    If you filled the x array (once) and the y array (once), then print them both (once) you should see what you expect (I guess).

    > scanf("&#37;d");
    Where is it going to store the result?
    See the FAQ for some good ways of pausing a console program at the end.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    2
    I just use the scanf at the end temporarily to make the program stay open when I use compile and run. Just a habit I suppose I should get out of.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Just a habit I suppose I should get out of.
    You should get out of habit to pass wrong number of argumants to scanf/printf not corresponding to format - it is the straight way to crash your program
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Doubts regarding random numbers generation
    By girish1026 in forum C Programming
    Replies: 9
    Last Post: 12-31-2008, 10:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  4. 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
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM