random generators

This is a discussion on random generators within the C Programming forums, part of the General Programming Boards category; I wanted to generate a floating point random number and i read about the functions drand48, erand48, are these functions ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    70

    random generators

    I wanted to generate a floating point random number and i read about the functions drand48, erand48, are these functions paired and are analogous to the functions srand & rand.

    i wrote the following program

    Code:
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    
    int main(void)
    {
    	int i;
    	int number;
    	float random_value;
    	float float_number;
    
    	printf("What is the number");
    	scanf("%d",&number);
    
    
    	drand48((unsigned int)time(NULL));
    
    	for(i=0;i<10;i++)
    	{
    		random_value=erand48(); 
    		float_number=random_value/(float)number;
    		printf("\n%f",float_number);
    	}
    
    
    
    
    	getch();
    	return 0;
    }
    but got the following errors

    Linking...
    test2.obj : error LNK2001: unresolved external symbol _erand48
    test2.obj : error LNK2001: unresolved external symbol _drand48

    please help

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Well, have you declared the drand48 and erand48 functions?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >LNK2001
    That's a VC++ linker error. VC++ doesn't support drand48 or erand48.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    70
    oh thanks prelude.
    how did you know LNK2001 is a VC++ link error only.

    would i need to declare the prototypes of the functions in the begining?

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Since VC++ doesn't support those functions, you're either going to have to get a compiler that does, or use different functions.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >how did you know LNK2001 is a VC++ link error only.
    I used to see it all too often.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    34
    Prelude,

    "I used to see it all too often. "

    What is your compiler of choice now (for Windows and for Linux)?

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >What is your compiler of choice now (for Windows and for Linux)?
    I use several popular compilers with no preference of one over the others.
    My best code is written with the delete key.

  9. #9
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by studentc
    I wanted to generate a floating point random number and i read about the functions drand48, erand48, are these functions paired and are analogous to the functions srand & rand.
    The problem is that these functions (*48) are not standard. Your compiler may have them or not. Even if they have them, they could have a different interface and/or behaviour. Use standard functions unless there is no standard wayof doing the job.

    What's wrong with the standard srand() and rand() functions ?
    Last edited by Emmanuel Delaha; 06-13-2004 at 04:38 AM. Reason: Bad wording

  10. #10
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by studentc
    how did you know LNK2001 is a VC++ link error only.
    On VC++, "LNKxxx" means LiNKer error.

  11. #11
    Registered User
    Join Date
    May 2004
    Posts
    70
    the standard srand() and rand() functions dont produce floating point random nubers, at least that is what i understand

    will they be supported by gcc,

    i thought stdlib was a standard library which would contain these functions, so do you mean to say that there are different versions of sdtlib.h?

  12. #12
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Quote Originally Posted by studentc
    the standard srand() and rand() functions dont produce floating point random nubers, at least that is what i understand

    will they be supported by gcc,

    i thought stdlib was a standard library which would contain these functions, so do you mean to say that there are different versions of sdtlib.h?
    It is easy to get floating point random numbers from rand() .
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int
    main(void)
    {
    	unsigned int seed=123;
    	double d;
    	int i;
    	srand(seed);
    	for(i=0;i<100;i++)
    	{
    		d= rand()/(RAND_MAX+1.0);
    		printf("d=%f\n",d);
    	}
    	return 0;
    }
    [added later]
    The implementation of rand() function that comes with most standard libraries is not that good, you many want to have a look at specialized libraries like gsl . See http://sources.redhat.com/gsl/
    Last edited by pinko_liberal; 06-13-2004 at 08:50 AM.
    The one who says it cannot be done should never interrupt the one who is doing it.

  13. #13
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by studentc
    the standard srand() and rand() functions dont produce floating point random nubers, at least that is what i understand

    will they be supported by gcc,

    i thought stdlib was a standard library which would contain these functions, so do you mean to say that there are different versions of sdtlib.h?
    As I said before, srand() and rand() are standard C functions. They are supported by all (so-called) conforming implementations of the C-language, including gcc.

    <stdlib.h> is not a library. It's an interface file (actually a prepocessor directive) that supplies the necessary interfaces to use properly a lot of functions of the standard C library.

    BTW, what is a floating point pseudo random number ?

    Because rand() returns an integer between 0 and RAND_MAX - 1, just divide the number given by rand() by RAND_MAX, and you have a fp value between 0.0 and 1.0 - EPSILON.
    Code:
    #include <stdlib.h>
    #include <time.h>
    #include <stdio.h>
    
    int main (void)
    {
       int     i;
    
       srand ((int) time (NULL));
    
       for (i = 0; i < 10; i++)
       {
          double  n = rand () / (double) RAND_MAX;
    
          printf ("n = %f\n", n);
       }
    
       return 0;
    }
    Emmanuel Delahaye

    "C is a sharp tool"

  14. #14
    Registered User
    Join Date
    May 2004
    Posts
    70
    i tried the RAND_MAX but i got the following
    test.c:13: `RAND_MAX' undeclared (first use in this function)

    i wrote the following function
    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[

    please help

  15. #15
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    RAND_MAX defined in stdlib.h

Page 1 of 2 12 LastLast
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, 09:09 PM
  2. Lesson #3 - Math
    By oval in forum C# Programming
    Replies: 2
    Last Post: 04-27-2006, 09: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, 05:11 PM

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