Quick! where the hell is random () (VC + + question) [time is of the essence] [random numbers]

Q: I have a random number between 0 and 13, after checking several textbooks C + +, I came across random (int n), where the range of is [0, (n-1)]. So after checking the MSDN documentation and not found anything useful at all, I ask if this function is even included in VC + +.

Time is the essence, this is due tomorrow.


Re:Originally posted by: guy
what the hell would "iRandom = (int)(13*rand()); " do anyways? what are you trying to do with that int? You dont need to write your own random number function. Everythign that these guys were saying is true, msvcc+ compiler has some weird non ansi things to it, but it still follows it fairly well. I don't understand why schools make people use Visual C++ and microsoft compilers anyways. I used to use Visual studio as an editor before I discovered teh wonders of Vim and figured out how to do syntax coloring, but I always compiled my homework on a unix system using g++. IMHO its a lot easier to learn the standard way to program then adapt to MS's quirks than the other way around.

That's true, I had fun with some string function that was broken (fin.getstring() or some such, I don't remember and the program that used it was wiped out when I fried my computer last friday). I had to get character arrays from the file and then convert them to strings. A pain in the ass for something that's supposed to work as documented. Anyway, I didn't need to write my own rand() function, just figure out a way to make rand() only return integers between 0 and 13, which ended up being 3 lines of code, so not too hard.


Re:Originally posted by: guy
what the hell would "iRandom = (int)(13*rand()); " do anyways? what are you trying to do with that int?

Whoa, calma amigo :)

If your random function outputs a double value between 0 and 1, and if you multiply this value by n+1 and round it to an int, you'll obtain an integer between 0 and n. Capich?
I was wrong though and rand outputs integers between 0 and RAND_MAX, so an alternative to the modulo solution suggested here would be (supposing RAND_MAX is defined):

(int)((double)rand()/RAND_MAX*14.0)


Re:Originally posted by: guy
what the hell would "iRandom = (int)(13*rand()); " do anyways? what are you trying to do with that int? You dont need to write your own random number function. Everythign that these guys were saying is true, msvcc+ compiler has some weird non ansi things to it, but it still follows it fairly well. I don't understand why schools make people use Visual C++ and microsoft compilers anyways. I used to use Visual studio as an editor before I discovered teh wonders of Vim and figured out how to do syntax coloring, but I always compiled my homework on a unix system using g++. IMHO its a lot easier to learn the standard way to program then adapt to MS's quirks than the other way around.

What problems have you had with VC++? Where did it differ from the 'standard'?


Re:what the hell would "iRandom = (int)(13*rand()); " do anyways? what are you trying to do with that int? You dont need to write your own random number function. Everythign that these guys were saying is true, msvcc+ compiler has some weird non ansi things to it, but it still follows it fairly well. I don't understand why schools make people use Visual C++ and microsoft compilers anyways. I used to use Visual studio as an editor before I discovered teh wonders of Vim and figured out how to do syntax coloring, but I always compiled my homework on a unix system using g++. IMHO its a lot easier to learn the standard way to program then adapt to MS's quirks than the other way around.

Re:Originally posted by: guy

Originally posted by: guy
Yes, the function is called rand() and is located in stdlib.h. Remember that you'll need to call srand() with a seed or else you'll be getting the same number each time you call rand(). A good way to do this is to include time.h and seed the random number generator with time(). Here's an example of how to get a different random number in [0..13]:

(after including time.h and stdlib.h)
int iRandom;
srand(time());
iRandom = rand() % 14;

Remember that % is the modulo operator. It divides the l-value by the r-value and returns the remainder. If you mod by 14 you'll receive a result in [0..13].

I could be wrong, but I thought rand() returned a value between 0 and 1. Following your example, I would then replace your last line by:

iRandom = (int)(13*rand());

Yes, guy is right. On pretty much every C/C++ compiler, rand() has an integer return value. RAND_MAX will vary, but normally it's set to the maximum size of the return type. This varies from platform to platform, but on x86 systems, a signed int can be as high as 32767. So yeah, iRandom = rand() % 14; would work just fine.


Re:thanks for the help guys, I think I figured it out. I had to write my own random() function though.

Re:Originally posted by: guy

I could be wrong, but I thought rand() returned a value between 0 and 1. Following your example, I would then replace your last line by:

iRandom = (int)(13*rand());

Not on Win32/MSVC++

From MSDN Library:
rand
Generates a pseudorandom number.

int rand( void );

Remarks

The rand function returns a pseudorandom integer in the range 0 to RAND_MAX. Use the srand function to seed the pseudorandom-number generator before calling rand


Re:Originally posted by: guy
Yes, the function is called rand() and is located in stdlib.h. Remember that you'll need to call srand() with a seed or else you'll be getting the same number each time you call rand(). A good way to do this is to include time.h and seed the random number generator with time(). Here's an example of how to get a different random number in [0..13]:

(after including time.h and stdlib.h)
int iRandom;
srand(time());
iRandom = rand() % 14;

Remember that % is the modulo operator. It divides the l-value by the r-value and returns the remainder. If you mod by 14 you'll receive a result in [0..13].

I could be wrong, but I thought rand() returned a value between 0 and 1. Following your example, I would then replace your last line by:

iRandom = (int)(13*rand());


Re:Yes, the function is called rand() and is located in stdlib.h. Remember that you'll need to call srand() with a seed or else you'll be getting the same number each time you call rand(). A good way to do this is to include time.h and seed the random number generator with time(). Here's an example of how to get a different random number in [0..13]:

(after including time.h and stdlib.h)
int iRandom;
srand(time());
iRandom = rand() % 14;

Remember that % is the modulo operator. It divides the l-value by the r-value and returns the remainder. If you mod by 14 you'll receive a result in [0..13].


Re:It's called rand().

Re:Took ~10 seconds to find in Linux =)

RANDOM(3) Linux Programmer's Manual RANDOM(3)

NAME
random, srandom, initstate, setstate – random number generator.

SYNOPSIS
#include <stdlib.h>

long int random(void);
void srandom(unsigned int seed);
char *initstate(unsigned int seed, char *state, size_t n);
char *setstate(char *state);

DESCRIPTION
The random() function uses a non-linear additive feedback random number generator
employing a default table of size 31 long integers to return successive pseudo-random
numbers in the range from 0 to RAND_MAX. The period of this random number generator is
very large, approximately 16*((2**31)-1).

The srandom() function sets its argument as the seed for a new sequence of pseudo-ran*
dom integers to be returned by random(). These sequences are repeatable by calling
srandom() with the same seed value. If no seed value is provided, the random() func*
tion is automatically seeded with a value of 1.

The initstate() function allows a state array state to be initialized for use by ran*
dom(). The size of the state array n is used by initstate() to decide how sophisti*
cated a random number generator it should use — the larger the state array, the better
the will be. seed is the seed for the initialization, which specifies a
starting point for the random number sequence, and provides for restarting at the same
point.

The setstate() function changes the state array used by the random() function. The
state array state is used for random number generation until the next call to init*
state() or setstate(). state must first have been initialized using initstate() or be
the result of a previous call of setstate().

RETURN VALUE
The random() function returns a value between 0 and RAND_MAX. The srandom() function
returns no value. The initstate() and setstate() functions return a pointer to the
previous state array, or NULL on error.

ERRORS
EINVAL A state array of less than 8 bytes was specified to initstate().

NOTES
Current "optimal" values for the size of the state array n are 8, 32, 64, 128, and 256
bytes; other amounts will be rounded down to the nearest known amount. Using less than
8 bytes will cause an error.

CONFORMING TO
BSD 4.3

SEE ALSO
rand(3), srand(3)

GNU 2000-08-20 RANDOM(3)


Related posts

Leave a comment

0 Comments.

Leave a Reply


click to changeSecurity Code

[ Ctrl + Enter ]