Random() function is not working in C

In summary: 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?Yes, you can use rand_s() from a standard C library.
  • #1
number0
104
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
  • #2
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.
 
  • #3
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?
 
  • #4
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?
 
  • #5
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

  • Program_4_-_Gilligan.pdf
    135 KB · Views: 288
Last edited:
  • #6
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:
  • #7
I don't understand how this works in Unix, but not MSV -.-
 
  • #8
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.
 
  • #9
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
 

Related to Random() function is not working in C

1. Why is the Random() function not generating random numbers in my C code?

The Random() function in C is not truly random, but rather uses a pseudo-random number generator. This means that the numbers it generates follow a specific pattern and can be predicted. If you are getting the same sequence of numbers every time, it is likely that your code is not properly initializing the random number generator. Make sure to call the srand() function before using the Random() function to seed the generator with a different value each time.

2. Can I use the Random() function in a multi-threaded program?

Yes, the Random() function is thread-safe, meaning it can be used in multiple threads without causing issues. However, if your program requires a high level of randomness, it is recommended to use a different library or algorithm for generating random numbers.

3. Why does the Random() function sometimes give me negative numbers?

The Random() function returns a number within a specified range, which by default is from 0 to the maximum value of an integer. If you want to generate only positive numbers, you can use the modulus operator (%) to limit the range of numbers to be between 0 and a specified maximum value.

4. How can I generate a random number within a specific range using the Random() function?

The Random() function can be used to generate a random number within a specific range by using the modulus operator (%) to limit the range of numbers. For example, if you want to generate a random number between 1 and 10, you can use the expression (Random() % 10) + 1.

5. Is the Random() function truly random?

No, the Random() function in C is not truly random, but rather uses a pseudo-random number generator. This means that the numbers it generates follow a specific pattern and can be predicted. If you need a high level of randomness, it is recommended to use a different library or algorithm for generating random numbers.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
438
  • Programming and Computer Science
Replies
6
Views
951
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
4
Views
754
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
967
  • Programming and Computer Science
Replies
1
Views
663
Back
Top