![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4
![]() |
Confused about a random # generating tutorial.
hi i found this tutorial about random # generating tutorial.
http://www.cprogramming.com/tutorial/random.html It uses rand() and srand() and uses the time function to use the system clock seconds as a seed. What im confused about is the tutorial is named "Getting Random Values in C and C++ with Rand" but all the examples include iostream meaning they are C++. They use cstdlib and time.h are they standard C libraries and you can use them in C++ because (not sure how it is termed) it is a superset/subset of C? Is this the perfered method of random number generation in C? Also they say that RAND_MAX is defined by the compiler. What is the best way to find out what your compiler defines RAND_MAX as? Could you manipulate it by doing #define RAND_MAX (Whatever Number You Want as Max)? thanks for any replies in advance. |
|
|
|
|
|
#2 |
|
Professional Programmer
![]() Join Date: Sep 2005
Posts: 419
Rep Power: 3
![]() |
>but all the examples include iostream meaning they are C++
Yea, RoD has demonstrated an inability to distinguish C and C++ before. ![]() >Is this the perfered method of random number generation in C? It depends on what you're doing. For classwork, rand and srand seeded using time are sufficient, but for anything more sophisticated you'll probably want to look into more effective generators. Rather than tootle around on tutorials that may or may not be good (cprogramming.com tutorials are hit or miss even though the forums are pretty darn good), I have an article on rand and friends that could help you out a bit. The best part is that it doesn't confuse C and C++, and uses strict C. ![]() >What is the best way to find out what your compiler defines RAND_MAX as? Print it: #include <stdio.h>
#include <stdlib.h>
int main ( void )
{
printf ( "RAND_MAX = %d\n", RAND_MAX );
return 0;
}You can, but it might not work at all like you want. The C standard doesn't allow you to redefine standard names.
__________________
Even if the voices aren't real, they have some pretty good ideas. |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
C++ could be considered superset of C, as it's supposed to have backwards compatibility, it does kind of, although not all. It has a lot of advantages in comparison with C. If it's all the same to you, I suggest you take that. I like C better though because it doesn't have all that taurine excrement I never use anyway
![]()
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4
![]() |
Sometimes seeing what the rand() function does helps more than anything.
FWIW - Knuth created RANARRAY along time ago - see Seminumerical Algorithms in 'The Art of Programming'. He has since changed it to pass Marsaglia's famous "Die Hard" battery of tests. Most of the extant rand() implmentations are based on his first version. Including MS and ISO. It DOES NOT work for cryptography. The code is very simple to understand - i.e., interpret what the code does. The math behind it may not be obvious. This is the GNU-Linux take on Knuth's algorithm. /* Reentrant random function frm POSIX.1c.
Copyright (C) 1996, 1999 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#include <stdlib.h>
/* This algorithm is mentioned in the ISO C standard, here extended
for 32 bits. */
int
rand_r (unsigned int *seed)
{
unsigned int next = *seed;
int result;
next *= 1103515245;
next += 12345;
result = (unsigned int) (next / 65536) % 2048;
next *= 1103515245;
next += 12345;
result <<= 10;
result ^= (unsigned int) (next / 65536) % 1024;
next *= 1103515245;
next += 12345;
result <<= 10;
result ^= (unsigned int) (next / 65536) % 1024;
*seed = next;
return result;
}srand() puts a starting value into "seed". If you use the same starting value, the sequence of "random" numbers is the same. Changing the seed value everytime you start the program again, makes the sequences different. All the algorithm does: 1. use the existing seed value - stored in a global variable called "seed" 2. multiply, shift and xor values several times 3. place a new value into the global seed variable 4. return the sam,e valuer of seed as the new rand() result. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|