www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - random number generator

reply Michael P. <baseball.mjp gmail.com> writes:
Okay. so I'm trying to make a simple Guess the Number game in D, and I need to
know how to generate random number. (I'm pretty much done the other parts of
the game)
In C++, I would seed the random number generator with:
srand( time( 0 ) ); 
and the generate a random number from 1-10 with:
randomnumber = ( rand() % 10 ) + 1;

How would I go about doing the same thing in D?
-Michael P.
Jul 30 2008
parent reply "Koroskin Denis" <2korden gmail.com> writes:
On Wed, 30 Jul 2008 23:28:14 +0400, Michael P. <baseball.mjp gmail.com>  
wrote:

 Okay. so I'm trying to make a simple Guess the Number game in D, and I  
 need to know how to generate random number. (I'm pretty much done the  
 other parts of the game)
 In C++, I would seed the random number generator with:
 srand( time( 0 ) );
 and the generate a random number from 1-10 with:
 randomnumber = ( rand() % 10 ) + 1;

 How would I go about doing the same thing in D?
 -Michael P.

First, srand() and rand() are C functions and thus are directly available in D. Just import std.c.stdlib or tango.stdc.stdlib. But if you use Tango, you should use tango.math.Random class instead. For example, // this will set random seed and return random value uint randomValue = Random.shared.seed(someSeed /* optional */).next(); or in two steps: Random.shared.seed( someSeed /* optional */ ); uint randomValue = Random.shared.next(); In Phobos, there is std.random module which has void rand_seed(int seed, int index) and uint rand() methods.
Jul 30 2008
parent reply Michael P. <baseball.mjp gmail.com> writes:
Koroskin Denis Wrote:

 On Wed, 30 Jul 2008 23:28:14 +0400, Michael P. <baseball.mjp gmail.com>  
 wrote:
 
 Okay. so I'm trying to make a simple Guess the Number game in D, and I  
 need to know how to generate random number. (I'm pretty much done the  
 other parts of the game)
 In C++, I would seed the random number generator with:
 srand( time( 0 ) );
 and the generate a random number from 1-10 with:
 randomnumber = ( rand() % 10 ) + 1;

 How would I go about doing the same thing in D?
 -Michael P.

First, srand() and rand() are C functions and thus are directly available in D. Just import std.c.stdlib or tango.stdc.stdlib. But if you use Tango, you should use tango.math.Random class instead. For example, // this will set random seed and return random value uint randomValue = Random.shared.seed(someSeed /* optional */).next(); or in two steps: Random.shared.seed( someSeed /* optional */ ); uint randomValue = Random.shared.next(); In Phobos, there is std.random module which has void rand_seed(int seed, int index) and uint rand() methods.

Thanks for fast reply. :P Anyways, I tried importing the std.c.stdlib and the std.c.time to use those exact same functions, but I got errors trying to use time(0) as a argument. Forgot to mention I tried that. So, what would I put as an argument for rand_seed instead of time(0)? Can I just put in any number? Like 0? BTW, don't use Tango. -Michael P.
Jul 30 2008
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Michael P." <baseball.mjp gmail.com> wrote in message 
news:g6qg4m$1kvu$1 digitalmars.com...

 Thanks for fast reply. :P
 Anyways, I tried importing the std.c.stdlib and the std.c.time to use 
 those exact same functions, but I got errors trying to use time(0) as a 
 argument. Forgot to mention I tried that.

 So, what would I put as an argument for rand_seed instead of time(0)? Can 
 I just put in any number? Like 0?
 BTW, don't use Tango.

 -Michael P.

You don't have to call rand_seed unless you need a repeatable sequence of pseudorandom numbers. Phobos calls it upon program initialization for you.
Jul 30 2008
parent Michael P. <baseball.mjp gmail.com> writes:
Jarrett Billingsley Wrote:

 "Michael P." <baseball.mjp gmail.com> wrote in message 
 news:g6qg4m$1kvu$1 digitalmars.com...
 
 Thanks for fast reply. :P
 Anyways, I tried importing the std.c.stdlib and the std.c.time to use 
 those exact same functions, but I got errors trying to use time(0) as a 
 argument. Forgot to mention I tried that.

 So, what would I put as an argument for rand_seed instead of time(0)? Can 
 I just put in any number? Like 0?
 BTW, don't use Tango.

 -Michael P.

You don't have to call rand_seed unless you need a repeatable sequence of pseudorandom numbers. Phobos calls it upon program initialization for you.

Okay thanks! :D
Jul 30 2008
prev sibling parent Mike Parker <aldacron gmail.com> writes:
Michael P. wrote:

 Anyways, I tried importing the std.c.stdlib and the std.c.time to use those
exact same functions, but I got errors trying to use time(0) as a argument.
Forgot to mention I tried that.

FYI, the time function doesn't take a 'number'. It takes a pointer to a time_t. If the pointer is non-null, the function stores the returned value in the memory to which the argument points. It's common to call the function with a null parameter. In C, 0 is often used in code to represent null. This is not supported in D. So you have to call the function using the null keyword in place of the 0: time(null).
Jul 30 2008