Random Numbers

This is a discussion on Random Numbers within the C Programming forums, part of the General Programming Boards category; The program I have is as follows: Code: #include <stdio.h> #include <time.h> #include <stdlib.h> #define SIZE 4 int main() { ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 09:47 PM
  3. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 06: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, 09:14 PM

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