
Random Number Generator ??
Hi,
Can anybody help with this. I'm trying to generate 6 random numbers between
1 and 42. When I run it in DOS (!) I get the same number 6 times. Thing
is, if I step through it in the compiler it's fine ? Any ideas ??
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int recur(int iparam);
int lottonums[6];
int main()
{
int num1 = 42;
int retval=0;
int loop=0;
srand(num1);
/****************/
randomize();
for(loop=0;loop<6;loop++)
{
/*randomize();*/
srand(num1);
randomize();
retval=recur(num1);
lottonums[loop]=retval;
printf("retval = %d\t loop = %d\n", retval, loop);
}
return 0;
}
int recur(int iparam)
{
int num2=0;
randomize();
num2=random(iparam);
while(num2==0) // don't want it too be zero ...
{
num2=recur(iparam);
}
printf("in recur() : num2 = %d\t", num2);
return num2;
}

Re: Random Number Generator ??
What is <time.h> for? If it affects the srand() or randomize() functions,
I would be inclined to suspect the granularity of the clock available to
DOS (it presumably takes less than a second to run, even under DOS?). Why
do you need to reseed the number generator each time anyway? I'm not familiar
with srand() and randomize() (as you might have gathered), but are you sure
they work the way you think? I thought they generally gave back an answer
between 0 and 1, which you then had to multiply up to give your 1..42.
Hope this helps a bit.
Simon.
<pflynnirl@aol.com> wrote:
>
>Hi,
>Can anybody help with this. I'm trying to generate 6 random numbers between
>1 and 42. When I run it in DOS (!) I get the same number 6 times. Thing
>is, if I step through it in the compiler it's fine ? Any ideas ??
>
>#include <stdio.h>
>#include <stdlib.h>
>#include <time.h>
>
>int recur(int iparam);
>
>int lottonums[6];
>
>int main()
>{
> int num1 = 42;
> int retval=0;
> int loop=0;
> srand(num1);
> /****************/
> randomize();
> for(loop=0;loop<6;loop++)
> {
> /*randomize();*/
> srand(num1);
> randomize();
> retval=recur(num1);
> lottonums[loop]=retval;
> printf("retval = %d\t loop = %d\n", retval, loop);
> }
> return 0;
>}
>int recur(int iparam)
>{
> int num2=0;
> randomize();
> num2=random(iparam);
> while(num2==0) // don't want it too be zero ...
> {
> num2=recur(iparam);
> }
> printf("in recur() : num2 = %d\t", num2);
> return num2;
>}

Re: Random Number Generator ??
Thanks Simon for making me think !! I took out all of the
srand() and randomize() calls and it's almost working. I get 6 random numbers
but each time I run it it's the same 6 !!
More thinking to do .......hmmmmmm
;)

Re: Random Number Generator ??
rand() generates a "pseudorandom" number by taking a seed (a number you give
it) and applying some math to it. Therefore, if you give rand() the same
seed each time, you will get the same random number each time  like your
first program. The reason that you are getting the same random numbers each
time you run the program now is that you aren't seeding rand() at all. Therefore,
it is using the same default seed each time your program runs. The best
way (that anyone has come up with) to seed rand() is with the current system
time (that is why you include time.h).
Therefore, you should call srand() with the time:
srand( (unsigned) time( NULL ) );
but only once at the beginning of your program.
Patrick
<pflynnirl@aol.com> wrote:
>
>Thanks Simon for making me think !! I took out all of the
>srand() and randomize() calls and it's almost working. I get 6 random numbers
>but each time I run it it's the same 6 !!
>More thinking to do .......hmmmmmm
>
>;)
>

Re: Random Number Generator ??
I had the same problems. What Patrick is saying is correct. You originaly
'randomized' within your loop. And since time returns the number of seconds
that have ellapsed since 1/1/70 (I believe) you get the same number for the
duration of one second. Basically, do your srand() and randomize before
starting your loop. Then have the loop generate the random numbers.
"Patrick McConnell" <pmcconn@emory.edu> wrote:
>
>rand() generates a "pseudorandom" number by taking a seed (a number you
give
>it) and applying some math to it. Therefore, if you give rand() the same
>seed each time, you will get the same random number each time  like your
>first program. The reason that you are getting the same random numbers each
>time you run the program now is that you aren't seeding rand() at all.
Therefore,
>it is using the same default seed each time your program runs. The best
>way (that anyone has come up with) to seed rand() is with the current system
>time (that is why you include time.h).
>
>Therefore, you should call srand() with the time:
> srand( (unsigned) time( NULL ) );
>but only once at the beginning of your program.
>
> Patrick
>
>
><pflynnirl@aol.com> wrote:
>>
>>Thanks Simon for making me think !! I took out all of the
>>srand() and randomize() calls and it's almost working. I get 6 random
numbers
>>but each time I run it it's the same 6 !!
>>More thinking to do .......hmmmmmm
>>
>>;)
>>
>

Re: Random Number Generator ??
Thanks for the help guys, much appreciated !!
:)

Re: Random Number Generator ??
Hello I was wondering if you could send me what your code would look like
now, I am interested in random numbers as well and I am too starting out
on C++
<pflynnirl@aol.com> wrote:
>
>Thanks for the help guys, much appreciated !!
>
>:)

Re: Random Number Generator ??
Hi Jonathan,
Glad to be of assistance. The program works ok now, just needs a bit of
fine tuning to make sure there are no duplicates in the selection.
Patrick.
:)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int recur(int iparam);
int numarray[6];
time_t tm;
int main()
{
int num1 = 42;
int retval=0;
int loop=0;
srand((unsigned) time(NULL));
/****************/
for(loop=0;loop<6;loop++)
{
retval=recur(num1);
numarray[loop]=retval;
printf("retval = %d\t loop = %d\n", retval, loop);
}
return 0;
}
int recur(int iparam)
{
int num2=0;
num2=random(iparam);
while(num2==0)
{
num2=recur(iparam);
}
printf("in recur() : num2 = %d\t", num2);
return num2;
}

Re: Random Number Generator ??
Thanks patrick this will help alot =)
<pflynnirl@aol.com> wrote:
>
>Hi Jonathan,
>
>Glad to be of assistance. The program works ok now, just needs a bit of
>fine tuning to make sure there are no duplicates in the selection.
>
>Patrick.
>
>:)
>
>
>
>#include <stdio.h>
>#include <stdlib.h>
>#include <time.h>
>
>int recur(int iparam);
>
>int numarray[6];
>time_t tm;
>
>int main()
>{
> int num1 = 42;
> int retval=0;
> int loop=0;
> srand((unsigned) time(NULL));
> /****************/
> for(loop=0;loop<6;loop++)
> {
> retval=recur(num1);
> numarray[loop]=retval;
> printf("retval = %d\t loop = %d\n", retval, loop);
> }
> return 0;
>}
>int recur(int iparam)
>{
> int num2=0;
> num2=random(iparam);
> while(num2==0)
> {
> num2=recur(iparam);
> }
> printf("in recur() : num2 = %d\t", num2);
> return num2;
>}

Re: Random Number Generator ??
<pflynnirl@aol.com> wrote:
>
>Thanks Simon for making me think !! I took out all of the
>srand() and randomize() calls and it's almost working. I get 6 random numbers
>but each time I run it it's the same 6 !!
>More thinking to do .......hmmmmmm
>
>;)
>
Your code initializes numl to 42, and then uses that number to initialize
the random number generator. So every time the program gets executed, the
(pseudo)random number generator gets initialized with the same value, producing
the same 'random' values each time. if you for instance use this:
srand(time(NULL));
instead of your
srand(numl);
it will generate different random numbers each time
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Development Centers
 Android Development Center
 Cloud Development Project Center
 HTML5 Development Center
 Windows Mobile Development Center
