[comp.lang.c] random number, how do I do it?

root@ozdaltx.UUCP (root) (11/16/89)

I am trying to write some code to generate a random number that would
fall between 0 and 65,000.  I know there is something I'm missing
here, but can't seem to pin it down.  I feel like the problem is in
the type declarations, but have tried most combinations with not a lot
of success.  Any suggestions?

Sample code:
------------------------------
#include <stdio.h>
main()
{
    extern unsigned short rand();
    extern unsigned long time();
    unsigned short offset;

/* generate a random number using time and pid as seed */

   while(1){
    srand((unsigned)getpid() + (unsigned)time(NULL));
    offset = rand();
    printf("NUMBER=%ld\n",offset);
    sleep(1);
  }

}
------------------
Thanks
Scotty
AIDS INFORMATION EXCHANGE BBS      (214) 247-2367/247-5609
               "Education is the best weapon"
     {ames,rutgers,texsun,smu}!attctc!ozdaltx!sysop 

mike@relgyro.stanford.edu (Mike Macgirvin) (11/17/89)

In article <5726@ozdaltx.UUCP> root@ozdaltx.UUCP (root) writes:
>I am trying to write some code to generate a random number that would
>fall between 0 and 65,000.  I know there is something I'm missing
>#include <stdio.h>
>main()
>{
>    extern unsigned short rand();
>    extern unsigned long time();
>    unsigned short offset;
>    while(1){
>    srand((unsigned)getpid() + (unsigned)time(NULL));
>    offset = rand();
>    printf("NUMBER=%ld\n",offset);
                     ^
>    sleep(1);
>  }
>}


	"%ld" means long int. 'offset' is a short int. You said so.
I believe the behaviour you are seeking requires "%ud".


	Now I have to type in a bunch of junk for the line counter..
	hit 'n' to avoid it.

.
.
.
.
.
.
.
.
.
.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+  Mike Macgirvin              Relativity Gyroscope Experiment (GP-B)    +
+  mike@relgyro.stanford.edu   (36.64.0.50)                              +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

rhg@cpsolv.UUCP (Richard H. Gumpertz) (11/17/89)

In article <5726@ozdaltx.UUCP> root@ozdaltx.UUCP (root) writes:
>    extern unsigned short rand();

rand returns int.  If int != short in your implementation, that could
account for your problems. 

-- 
===============================================================================
| Richard H. Gumpertz rhg%cpsolv@uunet.uu.NET -or- ...uunet!amgraf!cpsolv!rhg |
| Computer Problem Solving, 8905 Mohawk Lane, Leawood, Kansas 66206-1749      |
===============================================================================

root@ozdaltx.UUCP (root) (11/22/89)

 I wish to say thanks to the many net.replys on this question. Sorry I
can't e-mail individual thanks to everyone.  Although the example I
gave was not very good, the answers I received back opened a few doors
for me in the deeper workings of C.  Learn something new every day.

Scotty

kenmoore@unix.cis.pitt.edu (Kenneth L Moore) (11/28/89)

In article <5726@ozdaltx.UUCP> root@ozdaltx.UUCP (root) writes:
>I am trying to write some code to generate a random number that would
>fall between 0 and 65,000.  

>------------------
>Thanks
>Scotty
>AIDS INFORMATION EXCHANGE BBS      (214) 247-2367/247-5609
>               "Education is the best weapon"
>     {ames,rutgers,texsun,smu}!attctc!ozdaltx!sysop 


Here is a program that will generate the numbers you need.

To change the range of responses just change the 65000 in 
the subroutine.

The ran48 is seeded with the time and a static multiplier is
used for when the calls are so close that system time has not
changed.

X-----X-----X-----X-----X-----X cut here X-----X-----X-----X-----X-----X-----X

#include <time.h>
main()
{

long timeofday;
int ind,test,num1;

timeofday = time(0);
printf("\n The time is: \n %s\n",ctime(&timeofday));
ind = 0;
while(ind++ != 15)
  {
  num1 = sixtyfive();
  printf(" The number is %d \n",num1);
  }
}

int sixtyfive()
{
  static int xxx;
  long seedval;
  double drand48(),x;
  int num;
  seedval = time(0)+(long)xxx;
  srand48(seedval);
  xxx = seedval*drand48();
  x = drand48();
  num = (int)(65000*x);
return(num);
}

Sign me:

KK    KK  EEEEEEEE  NN     NN  MM       MM   OOOOO    OOOOO   RRRRRR   EEEEEEEE
KK   KK   EEEEEEEE  NNN    NN  MMM     MMM  OOOOOOO  OOOOOOO  RR   RR  EEEEEEEE
KK  KK    EE        NNNN   NN  MMMM   MMMM  OO   OO  OO   OO  RR  RR   EE
KKKKK     EEEEE     NN NN  NN  MM MM MM MM  OO   OO  OO   OO  RRRRR    EEEEE
KKKKK     EEEEE     NN  NN NN  MM  MMM  MM  OO   OO  OO   OO  RR RR    EEEEE
KK  KK    EE        NN   NNNN  MM   M   MM  OO   OO  OO   OO  RR  RR   EE
KK   KK   EEEEEEEE  NN    NNN  MM       MM  OOOOOOO  OOOOOOO  RR   RR  EEEEEEEE
KK    KK  EEEEEEEE  NN     NN  MM       MM   OOOOO    OOOOO   RR    RR EEEEEEEE

kenmoore@unix.cis.pitt.edu (Kenneth L. Moore)

(Sorry about the long sig but inews won't recognise my .signature...
even with "chmod 777 .signature". Tsk. Tsk.)