Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 30-12-2009
Member
 
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...
Reply With Quote
  #2  
Old 30-12-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
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;
}
Reply With Quote
  #3  
Old 30-12-2009
Member
 
Join Date: May 2008
Posts: 2,293
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).
Reply With Quote
  #4  
Old 30-12-2009
Member
 
Join Date: May 2008
Posts: 2,008
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);
}
Reply With Quote
  #5  
Old 30-12-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
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); 
}
Reply With Quote
  #6  
Old 30-12-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
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;
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 02:27 AM.