Results 1 to 6 of 6

Thread: Random Numbers in C

  1. #1
    Join Date
    Aug 2006
    Posts
    287

    Random Numbers in C

    Which key function in generating random numbers in C or in C++ program. When I created a array of 5 numbers and assign that array to access random numbers using the rand() function to get a random value in program but it does not work...

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Random Numbers in C

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int
    main (int arg, char *argv[])
    {
       unsigned int isd = (unsigned int)time(NULL);  
      int j;
      for (j=0; j<5; j++)
      {
        printf ("ran[%d]= %u\n",
          i, ran ());
      }
      return 0;
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: Random Numbers in C

    A pseudorandom number generator (PRNG) is an algorithm for generating a sequence of numbers that approximates the properties of random numbers. Random number generators (pseudo-random, of course) in C; mostly fast one-liners for "inline" use (to eliminate call / return overhead).

  4. #4
    Join Date
    May 2008
    Posts
    2,012

    Re: Random Numbers in C

    Use the following code to produce random number in C.
    Code:
    include <stdio.h>
    #include <stdlib.h>
                                                                                                   
    main(int a, char **va)
    {
      double x;
    
      z = rand()/(RAND_MAX+1.0);
    
      printf("%g\n",z);
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Random Numbers in C

    Code:
    int nm = random.Next();
    
    The following code returns a random number less than 1000.
    
    
    int nm = random.Next(1000);
    
    The following code returns a random number between min and max:
    
    
    private int RandomNumber(int mn, int mx)
    {
    Random random = new Random();
    return random.Next(mn, mx); 
    }

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Random Numbers in C

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main ()
    {
      
      srand ( time(NULL) ); 
      printf("RND_MAX = ", RND_MAX);
      
      printf ("A number between 0 and RND_MAX : %d\n", rnd());
      
      printf ("A number between 0 and 99: %d\n", rnd() % 100);
      
      printf ("A number between 0 and 9: %d\n", rnd() % 10);
    
      return 0;
    }

Similar Threads

  1. Need C program code to count numbers of negative & positive numbers
    By Sarfaraj Khan in forum Software Development
    Replies: 5
    Last Post: 16-01-2010, 02:00 PM
  2. C sharp program to print random numbers
    By Prashobh Mallu in forum Software Development
    Replies: 5
    Last Post: 12-01-2010, 12:07 PM
  3. Function for two random numbers
    By Zool in forum Software Development
    Replies: 3
    Last Post: 03-12-2009, 12:39 PM
  4. Want an array of Random numbers
    By InterNetWorKed in forum Software Development
    Replies: 4
    Last Post: 03-11-2009, 02:17 PM
  5. Random numbers. Help
    By tetilda in forum Software Development
    Replies: 2
    Last Post: 15-09-2008, 05:52 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,241,869.49377 seconds with 16 queries