Random numbers generator

This is a discussion on Random numbers generator within the C Programming forums, part of the General Programming Boards category; Hello folks... I'm working on a program that will generate 100 numbers in range of 1-999 and then there are ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    4

    Question Random numbers generator

    Hello folks...
    I'm working on a program that will generate 100 numbers in range of 1-999 and then there are few things I have to do....
    Here is what I have so far and I'm hoping that someone can explain further to me...

    a. Print the odd values, ten to a line
    b. Return the count of the number of even values
    c. Return the location of the smalles value in the array

    Now, so far I got program that generates numbers, I have code that will print 10 to a line but I'm stuck on the loop that will check for odd values and print them to line... Please dissregard b and c and help me with a, I'll try to work on the rest...

    Thanks a lot,

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    #define SIZE 100



    int main (void)
    {

    int number[SIZE];
    int i;
    int lineCount = 0;


    double tmp;


    srand(time( NULL ));


    for(i =0; i < SIZE; i++)
    {
    tmp=(double)rand()/((double)RAND_MAX+1);

    number[i] = (int)(999*tmp+0.5);
    } // Generates numbers in the range 1-999



    for(i=0; i < SIZE; i ++)
    {
    printf(" %d",number[i]);
    lineCount++;
    if (lineCount > 9)

    {
    printf("\n\n");
    lineCount = 0;
    }

    } // printArray a. Print the array ten values to a line.


    return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Most of use do it the easy way:

    rand() % 1000;

    But then, most of use actually read the FAQ and sticky posts here, and search the board when we have questions...

    Use code tags.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    35
    you can test if the number is a odd number with this:

    if (number % 2) != 0
    printf ("odd");

  4. #4
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Hmm.... See if you can adjust any of this to meet your needs.

    Code:
    #include <stdio.h>
    #include <stdlib.h> 
    #include <time.h>
    
    int main(void)
    {
      int i, nums[100], count;
      char key;
      
      count = 1;
      
      srand((unsigned)time(NULL));
        
      for(i=0; i < 100; i++)
       	nums[i] = rand() % 1000 + 1;
      
      for(i=0; i < 100; i++)
      {
         if((nums[i] % 2) != 0)
         {
       	if(count == 10)
       	{
         	  printf("\npress enter...\n");
       	  scanf("%*c", &key);
       	  count = 1;
       	 }
       		
       	printf("nums[%d] was odd -> %d\n", i, nums[i]);
            count++; 
        } 
      }	      
      	
      return EXIT_SUCCESS;
     }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    4
    Ronin,
    Thats a nice code you got there.... Works real nice...
    What I really need help with is the count of even numbers....
    After the numbers are generated and listen I need to get
    exact count of how many even numbers (2,4 etc) are there....

    And I also need to find the location of the smallest number, not the number but physical location in the array....
    Any ideas how to do this???

    Thanks a lot

  6. #6
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Originally posted by murand
    What I really need help with is the count of even numbers....
    It's in the for loop.... you just don't see it yet
    *hint* add another variable for the even counter.

    And I also need to find the location of the smallest number, not the number but physical location in the array....
    Any ideas how to do this???Thanks a lot
    not too bad... consider the following

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int array[] = { 5, 7, 2, 4, 1, 3, 2 };
      int found, i, lowest;
      
      lowest = 999;
      
      for(i=0; i < 7; i++)
      {
         if(lowest > array[i])
         {
              lowest = array[i];
              found = i;
         }
      }
      
      for(i=0; i < 7; i++)
          printf("array[%d] had %d. \n", i, array[i]);
      
      printf("\narray[%d] had the smallest value -> %d", 
            found, array[found]);	
      	
      return EXIT_SUCCESS;
     }
    There are potential problems such as two or more elements having the same lowest value. If that is the case then you would need to write the appropriate fix.

    It's late for me now... 3 am so my brain is 99% zonked. Hopefully that will compile.

    HTH
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    4
    quote
    It's in the for loop.... you just don't see it yet
    *hint* add another variable for the even counter.



    Ronin, In which for loop???? I went through all program and I couldnt get it going....

    Thanks man,

    MD

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    4
    Ronin, thanks for help...I figured the rest out and here is my code...

    MD



    /* This program randomly generates 100 numbers each time that is run.
    After the numbers are generated it will:

    a. Print the array ten values to a line. The values will be aligned.
    b. Print the odd values, ten to a line.
    d. Return a count of the number of even values.
    f. Return the location of the smallest value in the array.



    */



    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    #define SIZE 100



    int main (void)
    {

    int number[SIZE];
    int i;
    int lineCount = 0;
    int numOdd;
    int numEven = 0;
    int found;
    int lowest;

    double tmp;


    srand(time( NULL ));

    printf(" Randomly Generated Numbers Are:\n\n");
    printf(" ================================================== =========\n\n");
    for(i = 0; i < SIZE; i++)
    {
    tmp=(double)rand()/((double)RAND_MAX+1);

    number[i] = (int)(999*tmp+0.5);
    printf(" *%d*",number[i]);
    lineCount++;
    if (lineCount > 9)
    {
    printf("\n\n");
    lineCount = 0;
    }
    } // a. Generates numbers in the range 1-999 and prints the array ten values to a line.


    printf(" ================================================== =========\n\n");
    printf(" The odd numbers are:\n\n");
    printf(" ================================================== =========\n\n");
    for (i=0;i < SIZE; i++)
    {
    numOdd++;

    if (number[i] % 2)
    {
    printf(" *%d*", number[i]);
    lineCount++;
    }

    if (lineCount > 9)
    {
    printf("\n\n");
    lineCount = 0;
    }

    } // b. Print the odd values, ten to a line.
    printf("\n\n");
    printf(" ================================================== =========\n\n");



    for (i = 0; i < SIZE; i++)
    {
    if (number[i] %2 ==0)
    {
    numEven++;
    }
    }// d. Return a count of the number of even values.

    printf(" There are %d even numbers.\n\n", numEven);


    printf(" ================================================== =========\n\n");

    lowest = 999;
    for(i=0; i < SIZE; i++)
    {
    if(lowest > number[i])
    {
    lowest = number[i];
    found = i;
    }
    }

    for(i=0; i < 1; i++)

    printf(" Array[%d] had the smallest value of %d.\n\n", found, number[found]);
    printf(" ================================================== =========\n\n\n");
    // f. Return the location of the smallest value in the array.





    return 0;

    }

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Use code tags when posting code. You can use the "edit" button to change your previous post.
    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. 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. preventing same random numbers in JAVA
    By Rumproast23 in forum Tech Board
    Replies: 10
    Last Post: 04-04-2007, 06:46 AM
  3. Help generating multiple random numbers
    By d-dub in forum C++ Programming
    Replies: 7
    Last Post: 10-30-2006, 12:00 PM
  4. random numbers
    By ballmonkey in forum C++ Programming
    Replies: 3
    Last Post: 01-18-2005, 01:16 PM
  5. Good Random Number Generator
    By MethodMan in forum C Programming
    Replies: 4
    Last Post: 11-18-2004, 05:38 AM

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