Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 30th, 2005, 1:31 PM   #1
linuxpimp20
Hobbyist Programmer
 
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4 linuxpimp20 is on a distinguished road
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.
linuxpimp20 is offline   Reply With Quote
Old Nov 30th, 2005, 2:19 PM   #2
Narue
Professional Programmer
 
Narue's Avatar
 
Join Date: Sep 2005
Posts: 419
Rep Power: 3 Narue is on a distinguished road
>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;
}
>Could you manipulate it by doing #define RAND_MAX (Whatever Number You Want as Max)?
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.
Narue is offline   Reply With Quote
Old Nov 30th, 2005, 2:40 PM   #3
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Dec 1st, 2005, 12:12 PM   #4
jim mcnamara
Hobbyist Programmer
 
Join Date: Jun 2005
Location: New Mexico
Posts: 228
Rep Power: 4 jim mcnamara is on a distinguished road
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.
jim mcnamara is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:28 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC