FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Random Integers

 
Post new topic   Reply to topic     Forum Index -> General
View previous topic :: View next topic  
Author Message
ViolentAJ



Joined: 05 Feb 2009
Posts: 56

PostPosted: Fri Apr 17, 2009 6:38 pm    Post subject: Random Integers Reply with quote

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.
Back to top
View user's profile Send private message
michaelp



Joined: 27 Jul 2008
Posts: 114

PostPosted: Fri Apr 17, 2009 7:20 pm    Post subject: Reply with quote

http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=14190
Back to top
View user's profile Send private message
ViolentAJ



Joined: 05 Feb 2009
Posts: 56

PostPosted: Fri Apr 17, 2009 11:21 pm    Post subject: Reply with quote

Michael, I tried to click the replies to that linkt hat you gave me, but they lead to some other thread...
Back to top
View user's profile Send private message
michaelp



Joined: 27 Jul 2008
Posts: 114

PostPosted: Sat Apr 18, 2009 6:58 am    Post subject: Reply with quote

Gee, the same thing happens to me. Razz
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.
Back to top
View user's profile Send private message
ViolentAJ



Joined: 05 Feb 2009
Posts: 56

PostPosted: Sat Apr 18, 2009 6:53 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
ViolentAJ



Joined: 05 Feb 2009
Posts: 56

PostPosted: Sat Apr 18, 2009 7:01 pm    Post subject: Reply with quote

Nevermind. I just had to use null instead of NUL with all caps. Problem solved for now lol thanks for your help.
Back to top
View user's profile Send private message
michaelp



Joined: 27 Jul 2008
Posts: 114

PostPosted: Sun Apr 19, 2009 8:54 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
ViolentAJ



Joined: 05 Feb 2009
Posts: 56

PostPosted: Sun Apr 19, 2009 4:22 pm    Post subject: Reply with quote

I used C's srand() and rand(). It was a bit easier for me to understand.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> General All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group