Random() function is not working in C

  • Thread starter Thread starter number0
  • Start date Start date
  • Tags Tags
    Function Random
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
9 replies · 6K views
number0
Messages
102
Reaction score
0
Hello all,

I am trying to use a random function that my professor gave me:double rnd(int a){return (double)((a * random()) % 1000000000) / 1000000000; }Yes, I did include <stdlib.h>, <stdio.h>, and <math.h>. However, my compiler (Microsoft Visual Studio 2010 C++) does not recognize it as a function! By the way, I am programming the code in C, not C++.

Does anyone have any suggestion to my dilemma? Thank you for your time and response.
 
Physics news on Phys.org
There's a function called rand(). It only returns a 16 bit value, so to use it for larger numbers, call it several times and shift the results to combine the returned values.
 
rcgldr said:
There's a function called rand(). It only returns a 16 bit value, so to use it for larger numbers, call it several times and shift the results to combine the returned values.

I tried doing that... but the result I am aiming for is extremely hard to get using rand(). In other words, I have to call it more than 100 times! Is there any other way?
 
There are specialized libraries for random number generation, but are you sure you really need them?

What are you trying to achieve, and why do you need to call the rand() so many times?
 
Borek said:
There are specialized libraries for random number generation, but are you sure you really need them?

What are you trying to achieve, and why do you need to call the rand() so many times?

Check the attachment for what I am trying to achieve. It's too complicated for me to simplify it down.
 

Attachments

Last edited:
Not that hard to name it - Monte Carlo determination of pi number value.

Yes, you need to call rand() zillions of times, no, no need to use different random number generator.

Why do you think calling rand() many times is a problem? Whole idea of this approach is that you need to draw as many points as possible.

Unfortunately, whoever wrote that document should spend some time reading C standard library specification.

Edit: actually, after playing a little bit with the code I started to wonder if it can be done with standard rand(). But obviously Visual C plays some tricks on me, so I can't say what's the problem.
 
Last edited:
I don't understand how this works in Unix, but not MSV -.-
 
It works on Unix because random() is a part of the standard C library on Unix. It doesn't work on MSV because the function random() doesn't exist in the MSV library.

Note that random() is not a part of the standard C library as specified by the standard while rand() is. Any compliant C/C++ environment must supply rand().

If you want to do anything serious with regard to Monte Carlo analysis you will avoid both rand() and random() at all costs. Both are incredibly bad random number generators, and this is particularly so for Monte Carlo techniques.
 
D H said:
It works on Unix because random() is a part of the standard C library on Unix. It doesn't work on MSV because the function random() doesn't exist in the MSV library.

Note that random() is not a part of the standard C library as specified by the standard while rand() is. Any compliant C/C++ environment must supply rand().

If you want to do anything serious with regard to Monte Carlo analysis you will avoid both rand() and random() at all costs. Both are incredibly bad random number generators, and this is particularly so for Monte Carlo techniques.

Despite your warnings (which I am grateful of), is there anyway to add random() from a standard C library into MSV library?
 
For Windows using Visual Studio, you could use rand_s() instead, which generates 32 bit numbers:

Code:
#define _CRT_RAND_S  // needed to enable rand_s() declaration
#include <stdlib.h>

// ...

unsigned int    number;
errno_t         err;

// ...

    err = rand_s(&number );

To keep most of the code common, you could add a define to rename it:

#define random rand_s