NO

Author Topic: Random Numbers  (Read 3762 times)

Hassan

  • Guest
Random Numbers
« on: September 29, 2004, 07:05:09 AM »
Hi
I'am having probelms generating ramdon numbers can someone please help I will very much appriciate if someone would write a samll program on it in that was i can learn by decomposing the code and learn it for my self.

JohnF

  • Guest
Re: Random Numbers
« Reply #1 on: September 29, 2004, 08:38:16 AM »
Quote from: "Hassan"
Hi
I'am having probelms generating ramdon numbers can someone please help I will very much appriciate if someone would write a samll program on it in that was i can learn by decomposing the code and learn it for my self.


I assume you mean by using the run time function rand()

Code: [Select]

for(int i = 0; i<16; i++)
{
    printf("%d\n", rand());
}

Or you can use a modulo for limiting output to a specified range.

for(int i = 0; i<16; i++)
{
    printf("%hhd\n", (char)(rand()%256));
}


One can also seed the pseudo-random number generator before using it.

srand(GetTickCount());

or

srand(1234);

John