Thread: RE: [Dev-C++] how to use "srand"
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Dave M. <Dav...@im...> - 2003-03-20 08:39:16
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"> <HTML> <HEAD> <META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; = charset=3Diso-8859-1"> <META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version = 5.5.2653.12"> <TITLE>RE: [Dev-C++] how to use "srand"</TITLE> </HEAD> <BODY> <P><FONT SIZE=3D2>> how to use srand() function. how to define the = seed for this.</FONT> <BR><FONT SIZE=3D2>> suppose i want to generate a random number = between 0 and 10. </FONT> <BR><FONT SIZE=3D2>> then what seed should i use...</FONT> </P> <P><FONT SIZE=3D2>The seed just determines in what order the random = numbers are produced, not what the numbers are. If you want to generate = a random number between 0 and 10, then you could do it like = this:</FONT></P> <P><FONT SIZE=3D2>number =3D rand() % 11;</FONT> </P> </BODY> </HTML> |
|
From: Daniel G. <sp...@ho...> - 2003-03-20 11:04:50
|
??? Are you sure? I thought the seed was used to make numbers 'more random'? Can you explain seeding a little bit more? >From: Dave McMinn <Dav...@im...> >To: dev...@li... >Subject: RE: [Dev-C++] how to use "srand" >Date: Thu, 20 Mar 2003 08:40:57 -0000 > _________________________________________________________________ Stay in touch with MSN Messenger http://messenger.msn.co.uk |
|
From: Zodiaq <zo...@ac...> - 2003-03-20 11:31:41
|
Hello Daniel and all Dev-C++ users, Thursday, March 20, 2003, 12:04:44 PM, Daniel wrote as follows: DG> ??? Are you sure? I thought the seed was used to make numbers 'more random'? DG> Can you explain seeding a little bit more? random numbers generated by rand() are generated by this algorithm: -take some number - here where srand() is used -add to "some number" A VERY BIG INT (constant value, I don't know exactly what it is) which generates new number which is bigger than biggest possible int value - when this happens "some number" new value is exactly what is over max_int Each time you use rand() second part is repeated and "some number" value returned... for this algorithm most important is value of "A VERY BIG INT"... there are also some more advanced algorithms in net, but this one is fast and for almost every case just enough - it generated "pseudo random" numbers... -- regards, Zodiaq |
|
From: Per W. <pw...@ia...> - 2003-03-20 11:57:42
|
The problem with the normal rand() is that the low bits of it are not very rando, so rand()%11 have a tendancy to generate a very ugly pattern. /Per W On Thu, 20 Mar 2003, Zodiaq wrote: > Hello Daniel and all Dev-C++ users, > > Thursday, March 20, 2003, 12:04:44 PM, Daniel wrote as follows: > > DG> ??? Are you sure? I thought the seed was used to make numbers 'more random'? > DG> Can you explain seeding a little bit more? > > random numbers generated by rand() are generated by this algorithm: > -take some number - here where srand() is used > -add to "some number" A VERY BIG INT (constant value, I don't know > exactly what it is) which generates new number which is bigger than > biggest possible int value - when this happens "some number" new value > is exactly what is over max_int > > Each time you use rand() second part is repeated and "some number" > value returned... for this algorithm most important is value of "A > VERY BIG INT"... there are also some more advanced algorithms in net, > but this one is fast and for almost every case just enough - it > generated "pseudo random" numbers... > > -- > regards, Zodiaq > > > > ------------------------------------------------------- > This SF.net email is sponsored by: Tablet PC. > Does your code think in ink? You could win a Tablet PC. > Get a free Tablet PC hat just for playing. What are you waiting for? > http://ads.sourceforge.net/cgi-bin/redirect.pl?micr5043en > _______________________________________________ > Dev-cpp-users mailing list > Dev...@li... > TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm > https://lists.sourceforge.net/lists/listinfo/dev-cpp-users > |
|
From: Ian H. <Ia...@ma...> - 2003-03-21 09:35:31
|
The following is a bit of C code that uses the current time as a seed.
It's an example I have so it may have bits you will need to modify
(Namespaces and .h files etc.)
But it does work.=20
/*
** Program to demonstrate the use of the rand function
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
int nCount;
int nRandom;
/* Seed random number generator with current time */
srand((unsigned)time(NULL));
/* Display 20 random positive integers */
for(nCount=3D0; nCount<20; nCount++)
{
nRandom=3Drand();
printf("%d ", nRandom);
}
=20
system("PAUSE");
return 0;
}
|
|
From: Ioannis V. <iv...@em...> - 2003-03-21 23:17:30
|
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Ian Hill
> Sent: Friday, March 21, 2003 11:35 AM
> To: dev...@li...
> Subject: RE: [Dev-C++] how to use "srand"
>
>
> The following is a bit of C code that uses the current time as a seed.
> It's an example I have so it may have bits you will need to modify
> (Namespaces and .h files etc.)
> But it does work.
>
>
> /*
> ** Program to demonstrate the use of the rand function
> */
> #include <stdio.h>
> #include <stdlib.h>
> #include <time.h>
>
> int main(int argc, char *argv[])
> {
> int nCount;
> int nRandom;
>
> /* Seed random number generator with current time */
> srand((unsigned)time(NULL));
There is no need for casting here. The time_t argument (a typedef of a
signed integral type) is implicitly converted to unsigned int.
>
> /* Display 20 random positive integers */
> for(nCount=0; nCount<20; nCount++)
> {
> nRandom=rand();
> printf("%d ", nRandom);
> }
>
> system("PAUSE");
> return 0;
> }
Regards,
Ioannis Vranos
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL: http://run.to/noicys
|