Random() function is not working in C

  • Thread starter Thread starter number0
  • Start date Start date
  • Tags Tags
    Function Random
Click For Summary
The discussion revolves around issues with using a random number generation function in C programming, specifically in Microsoft Visual Studio 2010. The original poster is attempting to implement a function provided by their professor but encounters problems because the function `random()` is not recognized in the MSV library, as it is not part of the standard C library. Instead, the standard function `rand()` is available, but it only generates 16-bit values, necessitating multiple calls to achieve larger numbers. Participants suggest that while calling `rand()` many times is common in Monte Carlo simulations, it may not be efficient. They caution against using both `rand()` and `random()` for serious Monte Carlo analysis due to their poor randomness quality. An alternative solution is proposed: using `rand_s()`, which generates 32-bit numbers and can be defined to replace `random()` for compatibility. This approach allows for better random number generation while maintaining code consistency.
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.
 
Technology 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?
 
  • #10
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
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
12K
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
1K
  • · Replies 17 ·
Replies
17
Views
8K
Replies
1
Views
2K
Replies
4
Views
5K