Random() function is not working in C

  • Thread starter Thread starter number0
  • Start date Start date
  • Tags Tags
    Function Random
Click For Summary

Discussion Overview

The discussion revolves around issues related to the use of the random number generation function in C, specifically the function random() and its compatibility with Microsoft Visual Studio. Participants explore alternatives and the implications of using different random number generators in the context of Monte Carlo simulations.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant describes an issue with the function random() not being recognized in Microsoft Visual Studio, despite including necessary headers.
  • Another participant suggests using the function rand() but notes its limitations in generating larger numbers, proposing to call it multiple times and combine the results.
  • A participant expresses frustration at needing to call rand() excessively to achieve desired results and seeks alternative methods.
  • Some participants mention specialized libraries for random number generation and question the necessity of using them for the participant's goals.
  • One participant identifies the context as a Monte Carlo method for estimating the value of pi, emphasizing the need for numerous random points.
  • Concerns are raised about the quality of random number generators like rand() and random() for serious Monte Carlo analysis, with suggestions to avoid them.
  • Another participant inquires about the possibility of integrating random() into the MSV library despite its absence in the standard library.
  • A suggestion is made to use rand_s() as an alternative for generating 32-bit numbers in Visual Studio, including a code snippet for implementation.

Areas of Agreement / Disagreement

Participants express differing views on the effectiveness and necessity of various random number generators, with no consensus reached on the best approach for the specific problem at hand.

Contextual Notes

The discussion highlights the limitations of the standard C library in different environments, the varying quality of random number generators, and the specific requirements of Monte Carlo simulations.

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
3K
  • · Replies 6 ·
Replies
6
Views
12K
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 17 ·
Replies
17
Views
10K
Replies
1
Views
2K
Replies
4
Views
5K