Thread: storing random number into array

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    74

    storing random number into array

    Code:
    for(int i = 0 ; i < SIZE ; i ++)
        {
            scanf("%d" , & selection[i]);
            srand((unsigned) time(&t));
            draw[i] = rand() % 50;  //feeling could be a problem with this line of code :::::
            
        }
    is it possible to do this. i am trying to get 6 different numbers stored into 6 elements of an array . this is the piece of the code i think there is a problem with. ie my program scans the numbers and then crashes at this point so think it could be something to do with the commented line?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Check the FAQ on random numbers: FAQ > Generate random numbers? - Cprogramming.com. You're doing something wrong with srand.

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    First of all, please provide a small but complete example. Information missing such as what SIZE is or the definition of selection[] and draw[], etc...

    Yes, your call to srand() is a problem. Always check the return value of scanf().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing a number in an array
    By C++ student in forum C++ Programming
    Replies: 4
    Last Post: 09-28-2009, 08:47 AM
  2. Replies: 2
    Last Post: 02-23-2008, 03:59 PM
  3. Storing random number in array(sequential search & bubble sort)
    By dukethacore in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2005, 08:28 AM
  4. Storing random number in array(sequential search & bubble sort)
    By dukethacore in forum C++ Programming
    Replies: 1
    Last Post: 07-20-2005, 07:23 AM
  5. Replies: 2
    Last Post: 01-04-2004, 05:52 PM