TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: JIM ROBIN
from: BORIS TERZIC
date: 1997-05-12 23:39:00
subject: Throw of dice

And the cosmic ballet continues...
 JR> To cut a long story short does anyone know of an algorithm that can
 JR> generate numbers between a lower bound value and an upper bound value
 JR> - in the case of the dice example 1 - 6.
Hmm, maybe this could be of use:
=== Cut ===
Chris Watts wrote:
>
> I'm just starting to program in C++ and I am having a hard time
> implementing the rand() function (from stdlib.h).  Would someone be able
> to ribble off a function or something where it would pick a random
> integer between 65 and 90 and return it to the main program? Thanks in
> advance.
The rand in stdlib is not very good, so I'll give you an alternative
method.
In the class, you can use the RangeGet() method to get your number.
i.e:  rand.RangeGet(65,90)
/////// main.cc ////////
#include 
#include 
// A random generator using two high prime numbers.
// The lower bits of the seed are not very random,
// so we shift it down.
class Rand_t
{
public:
    Rand_t() : seed(1) {}
    Rand_t(int s) : seed(s) {}
    enum prime_e { e_prime1 = 1313320807, e_prime2 = 656660401 };
    void SeedSet(int s) { seed = s; }
    unsigned int ValueGet() { return (seed = seed * e_prime1 + e_prime2)
>> 16; }
    int RangeGet( int min, int max ) { return (ValueGet() % (max-min)) +
min; }
private:
    unsigned int seed;
};
#define ARRAY_SIZE 8
// The main program tests the spread of the random numbers.
int main(int argc, char *argv[])
{
    int i;
    Rand_t rand( time(0) ); // Construct the randomizer with the timer,
    // to get a different result every time.
    // Setup an array of counters
    int numb[ARRAY_SIZE];
    for(i=0; i * Origin: MESS.24/24.7/7.Demoscene.Coding.SF.(+32-3-772.69.25) (2:292/8139)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.