Thread: random generators

  1. #16
    Quote Originally Posted by studentc
    i tried the RAND_MAX but i got the following
    test.c:13: `RAND_MAX' undeclared (first use in this function)
    Sure. You must include <stdlib.h>
    Emmanuel Delahaye

    "C is a sharp tool"

  2. #17
    Registered User
    Join Date
    May 2004
    Posts
    70
    RAND_MAX works like a charm
    however i was trying to write the function using drand48 and erans48, can you tell me what am i doing wrong

    Code:
    #include <stdio.h>
    #include <time.h>
    int main(void)
    {
      int number;
      float random_factor;
      float random_number;
      random_number=0;
      printf("What is the number?");
      scanf("%d",&number);
      drand48( (unsigned int)time(NULL));
      random_factor=erand48();
      random_number=random_factor*(float)number;
      printf("\n Number is %d",number);
      printf("\nRandom Factor is %f",random_factor);
      printf("\nRandom Number is %f",random_number);
      return 0;
     
    }
    but i get the following results
    What is the number?10

    Number is 10
    Random Factor is 0.000000
    Random Number is 0.000000[

  3. #18
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >can you tell me what am i doing wrong

    > drand48( (unsigned int)time(NULL));
    > random_factor=erand48();


    Sure, you should be using srand48() and drand48().
    Code:
      srand48( (unsigned int)time(NULL));
      random_factor=drand48();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. random to int?
    By psyadam in forum C# Programming
    Replies: 7
    Last Post: 07-22-2008, 08:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 08:16 AM
  3. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  4. External Random Number Generators
    By Trauts in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2004, 12:18 PM
  5. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM