public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* RE: Random number generator
@ 1998-01-07 14:50 Erric Gilbert
  0 siblings, 0 replies; 14+ messages in thread
From: Erric Gilbert @ 1998-01-07 14:50 UTC (permalink / raw)
  To: 'BMullenber', gnu-win32

	I don't know of any number generators for C/C++ but there is a
built in $RANDOM function into BASH. You might be able to incorperate
that some how.

> -----Original Message-----
> From:	BMullenber [SMTP:BMullenber@aol.com]
> Sent:	Wednesday, January 07, 1998 3:05 AM
> To:	gnu-win32@cygnus.com
> Subject:	Random number generator
> 
> Anyone have the source code for a random number generator that
> actually works?
> 
> I am using C++
> 
> I am attempting to get random numbers between 0-100 for a game I wish
> to make
> that requires some sort of randomness with values.
> 
> If anyone has 1 that can do this, please send it or contact me
> 
> Thanks
> 
> Brandon
> BMullenber@aol.com
> -
> For help on using this list (especially unsubscribing), send a message
> to
> "gnu-win32-request@cygnus.com" with one line of text: "help".
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Random number generator
  2002-05-03  4:30 Paul Dilip K NPRI
@ 2002-05-03  7:07 ` Tim Prince
  0 siblings, 0 replies; 14+ messages in thread
From: Tim Prince @ 2002-05-03  7:07 UTC (permalink / raw)
  To: Paul Dilip K NPRI, 'cygwin@cygwin.com'

On Friday 03 May 2002 04:30, Paul Dilip K NPRI wrote:
> I am trying to use the random number generator for my fortran routine- but
> both RAN and SRAND does not work. Is there any other random number
> generating function which works with g77.
>
> Dilip K. Paul
>
> mailto:pauldk@npt.nuwc.navy.mil <mailto:pauldk@npt.nuwc.navy.mil>
Are you trying to start a flame war in an unusual place?  Why not simply look 
at the list of functions in 'info g77,' or in libg2c.a or the source, or, for 
more amusement, browse the comp.lang.fortran archive?  You don't say whether 
you're interested in portability, quality, speed, or any number of relevant 
factors.  Maybe a little research would help you decide.
-- 
Tim Prince

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* RE: Random number generator
@ 2002-05-03  6:31 Robinow, David
  0 siblings, 0 replies; 14+ messages in thread
From: Robinow, David @ 2002-05-03  6:31 UTC (permalink / raw)
  To: 'cygwin@cygwin.com'; +Cc: 'pauldk@npt.nuwc.navy.mil'

http://gcc.gnu.org/ml/gcc-help/2002-02/msg00160.html

Please note that this is off-topic for this list.

-----Original Message-----
From: Paul Dilip K NPRI [mailto:PaulDK@Npt.NUWC.Navy.Mil]
Subject: Random number generator

I am trying to use the random number generator for my fortran routine- but
both RAN and SRAND does not work. Is there any other random number
generating function which works with g77.
Dilip K. Paul

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Random number generator
@ 2002-05-03  4:30 Paul Dilip K NPRI
  2002-05-03  7:07 ` Tim Prince
  0 siblings, 1 reply; 14+ messages in thread
From: Paul Dilip K NPRI @ 2002-05-03  4:30 UTC (permalink / raw)
  To: 'cygwin@cygwin.com'


[-- Attachment #1.1: Type: text/plain, Size: 272 bytes --]

I am trying to use the random number generator for my fortran routine- but both RAN and SRAND does not work. Is there any other random number generating function which works with g77.

Dilip K. Paul

mailto:pauldk@npt.nuwc.navy.mil <mailto:pauldk@npt.nuwc.navy.mil> 



 

[-- Attachment #1.2: Type: text/html, Size: 841 bytes --]

[-- Attachment #2: image001.gif --]
[-- Type: image/gif, Size: 3674 bytes --]

[-- Attachment #3: Type: text/plain, Size: 214 bytes --]

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/

^ permalink raw reply	[flat|nested] 14+ messages in thread

* RE: Random Number Generator
  2001-04-30 10:37 HRahman10
                   ` (3 preceding siblings ...)
  2001-04-30 11:35 ` egor duda
@ 2001-04-30 12:16 ` Harold Hunt
  4 siblings, 0 replies; 14+ messages in thread
From: Harold Hunt @ 2001-04-30 12:16 UTC (permalink / raw)
  To: HRahman10, cygwin

Beginning CS student?  Thought so :)

Let me teach you how to debug your program:
1) Change your line that calls rand () to:
	input1 = rand ();
2) Notice that random numbers are now generated.
3) Have your program print out the value of RAND_MAX:
	  printf ("RAND_MAX: %d\n", RAND_MAX);
4) Calculate the result, in integer math, when
	dividing any integer smaller than RAND_MAX by
	RAND_MAX.  (Hint: the answer is zero)
5) Realize that what you really wanted was something like:
	// Get random numbers from 0 and 9, inclusive
	input1 = rand () % 10;

	// Get random numbers from 1 and 10, inclusive
	input1 = 1 + rand () % 10;

That was your freebie, from now on, ask general programming questions to a
list that is more suited to answering such questions; or, better yet, read
some programming books :)

Harold


--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Random Number Generator
  2001-04-30 10:37 HRahman10
  2001-04-30 11:28 ` Jonathon Merz
  2001-04-30 11:34 ` Larry Hall (RFK Partners, Inc)
@ 2001-04-30 11:35 ` DJ Delorie
  2001-04-30 11:35 ` egor duda
  2001-04-30 12:16 ` Harold Hunt
  4 siblings, 0 replies; 14+ messages in thread
From: DJ Delorie @ 2001-04-30 11:35 UTC (permalink / raw)
  To: HRahman10; +Cc: cygwin

Sorry, fails on Linux also.  After fixing the bugs in your program
(missing stdio.h and stdlib.h) gcc complains about this:

r.c: In function `main':
r.c:11: warning: integer overflow in expression

rand() may return values close to the maximum allowable integer.
Multiplying them by 10 may cause overflow.  And, RAND_MAX+1 may not be
a valid integer (on Linux, RAND_MAX is INT_MAX, so RAND_MAX+1 is a
very negative number).

In short, your logic is flawed and the fact that it works "at
university" is pure coincidence.

--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Random Number Generator
  2001-04-30 10:37 HRahman10
                   ` (2 preceding siblings ...)
  2001-04-30 11:35 ` DJ Delorie
@ 2001-04-30 11:35 ` egor duda
  2001-04-30 12:16 ` Harold Hunt
  4 siblings, 0 replies; 14+ messages in thread
From: egor duda @ 2001-04-30 11:35 UTC (permalink / raw)
  To: HRahman10; +Cc: cygwin

Hi!

Monday, 30 April, 2001 HRahman10@aol.com HRahman10@aol.com wrote:

Hac> Hi!, I'm writing a program using the random number genrator
Hac> rand() defined in <stdlib.h>.  I've written the following program
Hac> to test it:   

Hac> void main()
Hac> {
Hac>   int input1, i;
Hac>   srand(time(NULL));
Hac>   for (i=0; i<20 ; i++)
Hac>     {
Hac>       input1 = rand() * 10 / (RAND_MAX + 1);
Hac>       printf("random numbers %i\n",input1);
Hac>     }
Hac> } 


Hac> BUT, the results I get are all zeros.  Is there something wrong
Hac> with the gcc compiler provided by cygwin, because I've tested
Hac> this program at university, and it outputs random numbers
Hac> correctly.   

your program is wrong. RAND_MAX == INT_MAX == 0x7fffffff on cygwin.
so you've got integer overflow, RAND_MAX + 1 == -2147483648

Egor.            mailto:deo@logos-m.ru ICQ 5165414 FidoNet 2:5020/496.19



--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Random Number Generator
  2001-04-30 10:37 HRahman10
  2001-04-30 11:28 ` Jonathon Merz
@ 2001-04-30 11:34 ` Larry Hall (RFK Partners, Inc)
  2001-04-30 11:35 ` DJ Delorie
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 14+ messages in thread
From: Larry Hall (RFK Partners, Inc) @ 2001-04-30 11:34 UTC (permalink / raw)
  To: HRahman10; +Cc: cygwin

At 01:37 PM 4/30/2001, you wrote:
>Hi!, I'm writing a program using the random number genrator rand() defined in <stdlib.h>.  I've written the following program to test it: 
>
>
>
>void main()
>{
>   int input1, i;
>
>   srand(time(NULL));
>   
>   for (i=0; i<20 ; i++)
>     {
>       input1 = rand() * 10 / (RAND_MAX + 1);
>       printf("random numbers %i\n",input1);
>
>     }
>} 
>
>
>BUT, the results I get are all zeros.  Is there something wrong with the gcc compiler provided by cygwin, because I've tested this program at university, and it outputs random numbers correctly.
>
>Please Help!
>
>Thank You!



Try the latest snapshot at www.cygwin.com.



Larry Hall                              lhall@rfk.com
RFK Partners, Inc.                      http://www.rfk.com
118 Washington Street                   (508) 893-9779 - RFK Office
Holliston, MA 01746                     (508) 893-9889 - FAX


--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Random Number Generator
@ 2001-04-30 11:28 Noel L Yap
  0 siblings, 0 replies; 14+ messages in thread
From: Noel L Yap @ 2001-04-30 11:28 UTC (permalink / raw)
  To: HRahman10; +Cc: Cygwin

Did you include stdlib.h?

Noel




HRahman10@aol.com on 2001.04.30 13:37:42

To:   cygwin@cygwin.com
cc:   (bcc: Noel L Yap)
Subject:  Random Number Generator




Hi!, I'm writing a program using the random number genrator rand() defined in
<stdlib.h>.  I've written the following program to test it:



void main()
{
  int input1, i;

  srand(time(NULL));

  for (i=0; i<20 ; i++)
    {
      input1 = rand() * 10 / (RAND_MAX + 1);
      printf("random numbers %i\n",input1);

    }
}


BUT, the results I get are all zeros.  Is there something wrong with the gcc
compiler provided by cygwin, because I've tested this program at university, and
it outputs random numbers correctly.

Please Help!

Thank You!

--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple






This communication is for informational purposes only.  It is not intended as
an offer or solicitation for the purchase or sale of any financial instrument
or as an official confirmation of any transaction. All market prices, data
and other information are not warranted as to completeness or accuracy and
are subject to change without notice. Any comments or statements made herein
do not necessarily reflect those of J.P. Morgan Chase & Co., its
subsidiaries and affiliates.


--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Random Number Generator
  2001-04-30 10:37 HRahman10
@ 2001-04-30 11:28 ` Jonathon Merz
  2001-04-30 11:34 ` Larry Hall (RFK Partners, Inc)
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 14+ messages in thread
From: Jonathon Merz @ 2001-04-30 11:28 UTC (permalink / raw)
  To: HRahman10; +Cc: cygwin

A number of problems here...

   The reason only zeroes are being output is due to everything being 
interpreted as an integer and truncating to 0 when it is less than 1.  The 
best bet make the 10 that you multiply 10.0 or else cast it as float or 
double.If you desire integer results, you can then cast input1 as int when you 
need it as such.

   Another potential problem is (RAND_MAX +1).  This overflows (gcc gives a 
warning on compile), essentially giving you (RAND_MAX * -1) on any system 
where RAND_MAX is equal to MAX_INT, which as far as I know is most systems. 
If this is deliberate, for clearer coding, it would be better to write it as 
the latter, in addition to the fact, that if the code is ever compiled on a 
system where RAND_MAX is different than MAX_INT, it will not wrap, or else it 
may wrap differently.

In short, the following line:

    input1 = rand() * 10 / (RAND_MAX + 1);

should become:

    input1 = rand() * (float)10 / (RAND_MAX);

or:

    input1 = rand() * (float)10 / (RAND_MAX * -1);

if the negation of RAND_MAX was deliberate.


Hope this helps.

Jon

> Hi!, I'm writing a program using the random number genrator rand() defined in <stdlib.h>.  I've written the following program to test it: 
> 
> 
> void main()
> {
>   int input1, i;

> 
>   srand(time(NULL));
>   
>   for (i=0; i<20 ; i++)
>     {
>       input1 = rand() * 10 / (RAND_MAX + 1);
>       printf("random numbers %i\n",input1);
> 
>     }
> } 
> 
> 
> BUT, the results I get are all zeros.  Is there something wrong with the gcc compiler provided by cygwin, because I've tested this program at university, and it outputs random numbers correctly.




-- 


-------------------------------------
  If you had a million Shakespeares,
  could they write like a monkey?


--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Random Number Generator
@ 2001-04-30 10:37 HRahman10
  2001-04-30 11:28 ` Jonathon Merz
                   ` (4 more replies)
  0 siblings, 5 replies; 14+ messages in thread
From: HRahman10 @ 2001-04-30 10:37 UTC (permalink / raw)
  To: cygwin

Hi!, I'm writing a program using the random number genrator rand() defined in <stdlib.h>.  I've written the following program to test it: 



void main()
{
  int input1, i;

  srand(time(NULL));
  
  for (i=0; i<20 ; i++)
    {
      input1 = rand() * 10 / (RAND_MAX + 1);
      printf("random numbers %i\n",input1);

    }
} 


BUT, the results I get are all zeros.  Is there something wrong with the gcc compiler provided by cygwin, because I've tested this program at university, and it outputs random numbers correctly.

Please Help!

Thank You!

--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Random number generator
  1998-01-07  1:06 BMullenber
@ 1998-01-08  7:58 ` Andrew Lipnitsky
  0 siblings, 0 replies; 14+ messages in thread
From: Andrew Lipnitsky @ 1998-01-08  7:58 UTC (permalink / raw)
  To: BMullenber <BMullenber@aol.c, Cygnus Solution GnuWin32 Mail List

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 380 bytes --]

BMullenber wrote:
Anyone have the source code for a random number generator
that actually works?

I am using C++

I am attempting to get random numbers between 0-100 for a game I wish
to make
that requires some sort of randomness with values.

If anyone has 1 that can do this, please send it or contact me
 
http://www.taygeta.com/random.htmlš
Random Number Generationš

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Random number generator
@ 1998-01-07 22:33 Patrick J. Fay
  0 siblings, 0 replies; 14+ messages in thread
From: Patrick J. Fay @ 1998-01-07 22:33 UTC (permalink / raw)
  To: BMullenber; +Cc: gnu-win32

Try:
PREINSTALLED /pfay/memory 262 > cat tst.c
#include <stdlib.h>
#include <math.h>
int rand100()
{
   double rmax=100.0/RAND_MAX;
   return((int)(rmax*(rand())));
}
int main()
{
   int i;

   for(i=0;i<10;i++)
   {
           printf("rand100=%d\n",rand100());
   }
   return 0;
}
PREINSTALLED /pfay/memory 263 > !gc
gcc tst.c -o tstc
PREINSTALLED /pfay/memory 264 > !./t
./tstc
rand100=51
rand100=17
rand100=30
rand100=53
rand100=94
rand100=17
rand100=70
rand100=22
rand100=49
rand100=12 

Patrick Fay, Ph.D., Intel Corp.            email:   pfay@co.intel.com
Los Alamos National Lab                    wk:         (505) 665-9141
CTI M.S. B296                              fax:        (505) 667-5921
Los Alamos NM 87545    ASCI-RED http://www.acl.lanl.gov/~pfay/teraflop

-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Random number generator
@ 1998-01-07  1:06 BMullenber
  1998-01-08  7:58 ` Andrew Lipnitsky
  0 siblings, 1 reply; 14+ messages in thread
From: BMullenber @ 1998-01-07  1:06 UTC (permalink / raw)
  To: gnu-win32

Anyone have the source code for a random number generator that actually works?

I am using C++

I am attempting to get random numbers between 0-100 for a game I wish to make
that requires some sort of randomness with values.

If anyone has 1 that can do this, please send it or contact me

Thanks

Brandon
BMullenber@aol.com
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2002-05-03 14:07 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-01-07 14:50 Random number generator Erric Gilbert
  -- strict thread matches above, loose matches on Subject: below --
2002-05-03  6:31 Robinow, David
2002-05-03  4:30 Paul Dilip K NPRI
2002-05-03  7:07 ` Tim Prince
2001-04-30 11:28 Random Number Generator Noel L Yap
2001-04-30 10:37 HRahman10
2001-04-30 11:28 ` Jonathon Merz
2001-04-30 11:34 ` Larry Hall (RFK Partners, Inc)
2001-04-30 11:35 ` DJ Delorie
2001-04-30 11:35 ` egor duda
2001-04-30 12:16 ` Harold Hunt
1998-01-07 22:33 Random number generator Patrick J. Fay
1998-01-07  1:06 BMullenber
1998-01-08  7:58 ` Andrew Lipnitsky

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).