Thread: randon numbers

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    34

    randon numbers

    Hi,
    Could someone look at this and tell me where my mistake is? Thanks. Mcorn
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    int Fill_It(int, int, int, int [20]);
    
    int i;
    int sample[20], sample2[20], number;
    int t=0;
    
    int main(void)
    {
      cout << "Please enter a seed number: " ;
      cin >> t;
      Fill_It(t, 1000, 2000, sample);
    
      cout << "Twenty random numbers from 1000 to 2000\n\n";
      cout << "Seed: " << t << endl;
    	  for(i = 0; i < 20; i++)
    		cout << sample[i] << endl;
    
      cout << "Please enter another seed number: " ;
      cin >> t;
      Fill_It(t, 0, 1000, sample2);
    
    	cout << "Twenty random numbers from 0 to 1000\n\n";
    	cout << "Seed: " << t << endl;
    	  for(i = 0; i < 20; i++)
    		 cout << sample2[i] << endl;
    
      return 0;
      }
    
    Fill_It(int seed, int low, int high, int test[20]){
    
    	srand(seed);
    	i = 0;
    	while(i <= 20){
    			number = rand() % high;
    			if((number >= low) && (number <= high)){
    					test[i] = number;
    					  i++;
    					  }
    			else
    				;
    
    	  }
    	return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Could someone look at this and tell me where my mistake is?
    What is this, a "Spot The Bug" competition ?!

    Tell us what you think is wrong to save us reading and debugging each line of your code....
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    34
    Hi,
    I'm sorry, I was tactless on my question! What I'm trying to do is generate random numbers between 1000 and 2000. Next, I'm generating random numbers between 0 and 1000. I can't get it to compile and I've been looking at it for the past hour or so. I thought if another person that is not aggrevated and with a fresh set of eyes might see something right away. I'm sure it is something very minor. ( at least I hope) thanks. Mcorn

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>while(i <= 20){
    This will overflow the buffer. You need
    >>while(i < 20){


    Read this too..

    There might be other problems... I haven't looked too hard
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing numbers to a list of numbers held in a text file
    By jmajeremy in forum C++ Programming
    Replies: 3
    Last Post: 11-06-2006, 07:56 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. My code wont generate randon numbers
    By n00b_101 in forum C++ Programming
    Replies: 4
    Last Post: 10-20-2005, 05:39 AM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 11:15 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM