Generating 1000 random integers

This is a discussion on Generating 1000 random integers within the C Programming forums, part of the General Programming Boards category; This is a little snippet of code I wrote. Out of 1000 random integers I need to choose 50. This ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    76

    Generating 1000 random integers

    This is a little snippet of code I wrote. Out of 1000 random integers I need to choose 50. This is what I wrote but it falls short. Any help?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main() {
    
    	int x, y, ia[50];
    	
    	for(x=0; x<50; x++) {
    		srand(time(NULL));
    		y=rand()%1000+1;
    		ia[x]=y;
    		printf("%d ", ia[x]);
    	}
    
    	return 0;
    		
    		
    }

  2. #2
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    If I understand correctly, then I believe your syntax is wrong. You are creating a loop and looping through 50. If you wanted to create 1000 random numbers, wouldn't you loop through 1000 and pick 50 out the of 1000 after they are generated?


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    It falls short, how exactly?

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    The problem with the code is that it generates 50 integers, but it is 50 of the same number.

  5. #5
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Do you have to choose the 50 random numbers randomly?

    You could store 1000 numbers in an array and then pick numbers from the array by stepping through the array by a random amount; making the maximum step such that you couldn't run out of array before you have 50 numbers (i.e. a maximum random step of 20?)
    Demonographic rhinology is not the only possible outcome, but why take the chance

  6. #6
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Oh, and seed rand outside your loop.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Quote Originally Posted by Azuth
    Oh, and seed rand outside your loop.
    Bingo!! Got it. Thanks all!!!!

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    De Ja Vu, indeed!

    Need some help or advice in C++ program

    It's in the C++ board, but I hope it's not too hard to understand the code and fix your own. Include statements are missing the .h, and some have a 'c' added before the name - those are header files available in C, and you'll recognize them. Ignore the "using ..." statements, and cin and cout are just for input / output, respectively... Good luck!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Generating random letters
    By ominub in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 02:12 AM
  2. Generating random 2D mountains/terrain
    By Flaug in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2009, 03:49 PM
  3. generating random integers from 0 to 17576999
    By chickenandfries in forum C Programming
    Replies: 11
    Last Post: 08-30-2008, 12:48 PM
  4. Making a random array of integers
    By Vidak in forum C# Programming
    Replies: 2
    Last Post: 11-09-2007, 06:00 AM
  5. Generating a random number?
    By Konspiracy in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 01:33 AM

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