LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-27-2002, 10:49 PM   #1
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Rep: Reputation: 30
I need random number in C ...


Hi. As a programming practice I am going to be making a simple text based game similiar to dungeons and dragons. I will need random numbers for damage from attacks, etc. Will using >

result = 1 + rand() % 20

give me a good result for say a number from 1-20?

Or is there a better way to get a random number then using rand()?

I will be programming in C ...

thanks
 
Old 10-28-2002, 02:20 AM   #2
sarin
Member
 
Registered: May 2001
Location: India, Kerala, Thrissur
Distribution: FC 7-10
Posts: 354
Blog Entries: 2

Rep: Reputation: 34
I found this from man page of rand

In Numerical Recipes in C: The Art of Scientific Computing
(William H. Press, Brian P. Flannery, Saul A. Teukolsky,
William T. Vetterling; New York: Cambridge University
Press, 1992 (2nd ed., p. 277)), the following comments are
made:
"If you want to generate a random integer between 1
and 10, you should always do it by using high-order
bits, as in

j=1+(int) (10.0*rand()/(RAND_MAX+1.0));

and never by anything resembling

j=1+(rand() % 10);

(which uses lower-order bits)."

Hope this helps.
--Sarin
 
Old 10-28-2002, 02:22 AM   #3
GtkUser
Member
 
Registered: Sep 2002
Location: Canada
Distribution: Redhat 9.0
Posts: 637

Rep: Reputation: 30
Code:
#include<stdio.h>

int main()
{
  int ix;
  srand(); //seed for random number generator

  for ( ix = 0; ix < 50; ++ix)
    printf("%d\n", rand() % 20 + 1);

  return 0;
}
I tested this and it worked, but there are better algorithms. This is the basic way to get a random number using the standard C library. Read the Link page for more information.
< http://www.mkssoftware.com/docs/man3/srand.3.asp >
 
Old 10-28-2002, 03:02 AM   #4
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
thanks guys
 
Old 10-28-2002, 04:37 AM   #5
gluon
Member
 
Registered: Jun 2001
Location: Switzerland
Distribution: SuSE 8.[012], Gentoo
Posts: 42

Rep: Reputation: 15
Hi purpleburple,

Try this :

double MRG32k3a () /* P. L'Ecuyer 's generator*/
{
long k;
double p1, p2;
/* Component 1 */
p1 = a12 * s11 - a13n * s10;
k = p1 / m1; p1 -= k * m1; if (p1 < 0.0) p1 += m1;
s10 = s11; s11 = s12; s12 = p1;
/* Component 2 */
p2 = a21 * s22 - a23n * s20;
k = p2 / m2; p2 -= k * m2; if (p2 < 0.0) p2 += m2;
s20 = s21; s21 = s22; s22 = p2;
/* Combination */
if (p1 <= p2) return ((p1 - p2 + m1) * norm);
else return ((p1 - p2) * norm);
}

It's one of the best random number generator

Hope it helps, CU

gluon
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Generate random number with C++ TruongAn Programming 5 11-09-2005 12:01 AM
random number generation sailu_mvn Programming 4 03-10-2005 02:10 PM
Random number generators blufire Programming 5 11-09-2004 06:02 PM
Random Number Game tearinox Programming 4 08-20-2004 10:12 PM
Python Random number sethgeekx86 Programming 4 06-10-2004 08:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration