From: "Marp" Newsgroups: comp.os.msdos.djgpp Subject: Re: -- Random question -- Date: Sat, 24 Jul 1999 17:06:28 -0400 Organization: Netcom Lines: 29 Message-ID: <7nd9so$a96@dfw-ixnews3.ix.netcom.com> References: <7nd0vu$igt$1 AT zingo DOT tninet DOT se> NNTP-Posting-Host: prn-nj3-08.ix.netcom.com X-NETCOM-Date: Sat Jul 24 4:06:32 PM CDT 1999 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2014.211 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2014.211 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Börje Granberg wrote in message news:7nd0vu$igt$1 AT zingo DOT tninet DOT se... > I know there are commands like rand() and random() but how do I get a random > number between 1 and 100? > > In BASIC it is easy because there you get a random (float) number between 0 > and 1 that you can multiplicate with 100 and add 1, but I dont see that > opportunity in C++ or have my eyes missed that part? > > > -- > /Börje You can use the remainder operator like this: number = (rand() % 101) + 1; This will generate a number from 1 to 100. You can also substitute rand() with random() which generates numbers that are more random, but random() isn't as portable. Don't forget to seed it at the start of your program like this: srand(time(0)); /* for rand() */ srandom(time(0)); /* for random() */