Author Message
ViolentAJ

Joined: 05 Feb 2009
Posts: 56

 Posted: Fri Apr 17, 2009 6:38 pm    Post subject: Random Integers Sorry. I took a look at an example, but it didn't work. I just want to know how to get something along the lines of Math.Random(100); which should give a random number between 0 and 99. Thanks. I just need to know what to import (std.random, I assum) and how to use it. Sorry for being a n00b. Thanks. I'm working on a rudimentary text-based RPG.
michaelp

Joined: 27 Jul 2008
Posts: 114

 Posted: Fri Apr 17, 2009 7:20 pm    Post subject: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=14190
ViolentAJ

Joined: 05 Feb 2009
Posts: 56

 Posted: Fri Apr 17, 2009 11:21 pm    Post subject: Michael, I tried to click the replies to that linkt hat you gave me, but they lead to some other thread...
michaelp

Joined: 27 Jul 2008
Posts: 114

Posted: Sat Apr 18, 2009 6:58 am    Post subject:

Gee, the same thing happens to me.
Here are the replies:

Jarrett Billingsley:

 Quote: lo + rand() % (hi - lo) assuming you're using Phobos, where rand() returns a number in the range [0, uint.max]. This will get you a number in the range [1000, 2000); that is, it will be at least 1000 and at most 1999. If you need 2000 to be one of the numbers generated, use 2001 as the hi.

Don:
 Quote: The random numbers will not be evenly distributed, though. You'll get more which are closer to 1000 than to 2000. (Imagine uint.max = 1000. Then 0 and 1000 map to 1000, but every other number has only one number which maps to it). Simple way to fix this is to ignore numbers from the non-uniform part: uint rmax = uint.max - (uint.max % (hi-lo)); uint r; do { r = rand(); } while (r>rmax); return lo + r % (hi - lo);

Hope that helps.
ViolentAJ

Joined: 05 Feb 2009
Posts: 56

Posted: Sat Apr 18, 2009 6:53 pm    Post subject:

Thanks. I think that the way to do it in C is much easier to understand though. The only problem is I get the following error.

This is my code:

 Code: //Random number generation. This is an important concept for game design. //There is no easy Math.Random() or Random class like in Java or ActionScript. module RandomNumberTest; import std.stdio; //We can import from the C standard library too import std.c.stdlib; import std.c.time; void main(string[] args){        //Seed the randomizer so that we get different values    srand(time(NULL));        //Generate the number    int number = rand();        writefln(number); }

This is the error:
 Code: C:\WorkingInD\Test\RandomNumberTest.d(15): Error: undefined identifier NULL C:\WorkingInD\Test\RandomNumberTest.d(15): function std.c.time.time (int*) does not match parameter types (int) C:\WorkingInD\Test\RandomNumberTest.d(15): Error: cannot implicitly convert expression (NULL) of type int to int*

Of course, I'm used to just calling Random in Java or Flash ActionScript.

Sadly, I cannot use the Tango library. Last time I tried to install it it destroyed everything. It also ruined my setup in Eclipse.
ViolentAJ

Joined: 05 Feb 2009
Posts: 56

 Posted: Sat Apr 18, 2009 7:01 pm    Post subject: Nevermind. I just had to use null instead of NUL with all caps. Problem solved for now lol thanks for your help.
michaelp

Joined: 27 Jul 2008
Posts: 114

 Posted: Sun Apr 19, 2009 8:54 am    Post subject: You didn't use std.random? It's the same function(rand) and I don't think you need to seed it. (Well, I haven't when I used it.) http://digitalmars.com/d/1.0/phobos/std_random.html
ViolentAJ

Joined: 05 Feb 2009
Posts: 56

 Posted: Sun Apr 19, 2009 4:22 pm    Post subject: I used C's srand() and rand(). It was a bit easier for me to understand.
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First
 All times are GMT - 6 Hours Page 1 of 1