Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
July 30, 2008 random number generator | ||||
---|---|---|---|---|
| ||||
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. |
July 30, 2008 Re: random number generator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michael P. | 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.
|
July 30, 2008 Re: random number generator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Koroskin Denis | 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.
|
July 30, 2008 Re: random number generator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michael P. | "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. |
July 30, 2008 Re: random number generator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | 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
|
July 31, 2008 Re: random number generator | ||||
---|---|---|---|---|
| ||||
Posted in reply to Michael P. | 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).
|
Copyright © 1999-2021 by the D Language Foundation