Thread: Having a problem with random numbers in array

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    2

    Question Having a problem with random numbers in array

    Hello everyone ..

    I've got a problem with the random numbers in the array .. because it's give me a big numbers not small numbers and it doesn't change the numbers !! why ?? **Sorry for my English**

    This is my code :

    Code:
    #include <stdio.h>     
    #include <stdlib.h>
    
    
    int main()
    {
     
        int arr[10];
        int sum = 0;
     
         for (int i=0; i<10; i++){
              arr[1] = rand() % 10;
            printf("arr[%d] = %d\n" , i , arr[i]);
        }
        for (int i=0; i<10; i++) {
            sum += arr[i];
        }
    
    
        printf("the sum = %d", sum);
    
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Man_Onaizah View Post
    because it's give me a big numbers not small numbers ...
    You're not correctly assigning the random values to the array:

    Code:
    arr[1] = rand() % 10;
    You want "i" here, not "1".

    Quote Originally Posted by Man_Onaizah View Post
    ... and it doesn't change the numbers !! why ??
    You need to seed the PRNG to get different values each run. See the FAQ: FAQ > Generate random numbers? - Cprogramming.com

  3. #3
    Registered User
    Join Date
    May 2016
    Posts
    2
    Quote Originally Posted by Matticus View Post
    You're not correctly assigning the random values to the array:

    Code:
    arr[1] = rand() % 10;
    You want "i" here, not "1".



    You need to seed the PRNG to get different values each run. See the FAQ: FAQ > Generate random numbers? - Cprogramming.com
    thaaaaaaaank u so much bro <3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Filling an array with random numbers....
    By Glassjaw in forum C Programming
    Replies: 5
    Last Post: 11-27-2015, 11:49 PM
  2. problem store random numbers in array
    By Xavi Camps in forum C Programming
    Replies: 7
    Last Post: 01-13-2015, 12:03 AM
  3. Random numbers into array
    By Gil Carvalho in forum C Programming
    Replies: 15
    Last Post: 05-31-2012, 03:14 PM
  4. Picking random numbers from an array
    By toyganozy in forum C Programming
    Replies: 2
    Last Post: 04-06-2012, 07:52 PM
  5. Unique Random Numbers in 2D Array
    By kssjbr in forum C++ Programming
    Replies: 2
    Last Post: 08-06-2003, 02:45 AM

Tags for this Thread