really terrible

A

ash

hi friends
i m trying to make a pogram that prints a random number(without any
pattern( every time we run.
can anyone suggest me the logic
i m seeking anwer for 6 months
thankx
 
R

Rahul Chandok

Hi,

I am including a small program for calculating the random number.

#include <stdio.h>
#include<sys/types.h>
#include<times.h>
#include <stdlib.h>

int randomgen();

int main()
{
time_t seed;
int i = 0; randomnumber = 0;

seed = time(NULL);
srand(seed);

for ( i = 0; 1 < 1000; i++)
{
randomnumber = randomgen();
printf("Random Number generated is %d\n",randomnumber);
}

}

int randomgen()
{
return rand();
}

HTH;
Rahul
 
K

Keith Thompson

Rahul Chandok said:
I am including a small program for calculating the random number.

#include <stdio.h>
#include<sys/types.h>

This isn't a standard header, but you don't seem to be using anything
from it anyway.
#include<times.h>

There's no such header. I presume you mean <time.h>.

If you're going to post code, please at least try compiling it first.
#include <stdlib.h>

int randomgen();

int main()

Ok, but "int main(void)" is better.
{
time_t seed;
int i = 0; randomnumber = 0;

There's no need to initialize either variable; both have values
assigned to them before they're used.
seed = time(NULL);
srand(seed);

What is the purpose of the intermediate variable "seed"? You could
just write "srand(time(NULL));".
for ( i = 0; 1 < 1000; i++)
{
randomnumber = randomgen();
printf("Random Number generated is %d\n",randomnumber);
}

}

int randomgen()
{
return rand();
}

What is the purpose of this function? Why not just call rand()
directly?
 
R

Rahul Chandok

Hi,

I guess, he just wants the code snippet as an example.
I added the progarm and variable just to explain him the meaning of
each and every call do.

I added the function just in case Just to explain that he doesn't have
to call time and srand again and again. And he juct needs to call it
once.

Hope it answers all your queries.

Cheers
Rahul
 
N

Niklas Norrthon

Madhav said:
Jack Klein wrote:



Yeah, Sorry for that.

But return values from rand() definitly "have a pattern" in most
implementations. (OP specificly asked about a random number generator
that "does not have a pattern").

True random functions are impossible without support from special
hardware, and "good" pseudo random functions are far from trivial
to implement.

By the way, there is a section in the FAQ about pseudo random number
generators.

/Niklas Norrthon
 
K

Keith Thompson

Rahul Chandok said:
I guess, he just wants the code snippet as an example.
I added the progarm and variable just to explain him the meaning of
each and every call do.

I added the function just in case Just to explain that he doesn't have
to call time and srand again and again. And he juct needs to call it
once.

Hope it answers all your queries.

You've hardly answered any of them, but that's ok.

One more question: have you read <http://cfaj.freeshell.org/google/>?
Please do so before you post again.
 
R

raghu

hi rahul can you please explain the program how it generates the random
numbers? why you have used sys/types and time header files ? and whats
does time_t do? thanks a lot in advance.happy new year
 
R

Rahul Chandok

Hi Raghu,

sys/types.h is used as it contains the defination of time_t
time_t is the return type of time() function.

This function first calculates the seed and to calculate the unique
seed i have used the time() function. As the time() gives the number of
seconds passed since jan 1 1970, so it gives the unique value.

Then srand function uses this seed for the new sequence of pseudo
random numbers to be returned by the subsequent call to rand()
function. If the seed is same then subsequent pseudo random numbers are
repeated.

Happy New year to you too in advance.

HTH
Rahul
 
R

Richard Bos

Quote context, damnit! Learn to use Google Broken Beta or get a
newsreader.
sys/types.h is used as it contains the defination of time_t

Not in C said:
As the time() gives the number of seconds passed since jan 1 1970,

You do not know this. It may be true on your system, but C does not
guarantee this, or just about anything else about the format of a
time_t. All you know is that it is a scalar type encoding time.

Richard
 
K

Keyser Soze

ash said:
hi friends
i m trying to make a pogram that prints a random number(without any
pattern( every time we run.
can anyone suggest me the logic
i m seeking anwer for 6 months
thankx
It seems that there is no solution for your problem.

No program has yet been found that uses a deterministic method to produce a
non-deterministic result that does not rely on some random property present
in nature.

By this I mean that it seems impossible to use just an algorithm to create a
truly random sequence.

There are many algorithms that produce a pseudo random sequence but these
always produce the same sequence when started from the same initial state or
seed.
 
C

Chuck F.

Rahul said:
sys/types.h is used as it contains the defination of time_t
time_t is the return type of time() function.

No it doesn't. Don't give misinformation here. There is no such
standard include file. However, time.h does exist.

And learn to quote before posting here again. The following sig
will help.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
R

Richard Bos

Niklas Norrthon said:
But return values from rand() definitly "have a pattern" in most
implementations. (OP specificly asked about a random number generator
that "does not have a pattern").

True random functions are impossible without support from special
hardware, and "good" pseudo random functions are far from trivial
to implement.

Apart from that, any real RNG and any good PRNG will appear to have a
pattern to the sufficiently insistent observer, even if mathematically
it does not. Humans are just too good at pattern recognition; we will
recognise patterns even where there are none.

Richard
 
O

osmium

ash said:
i m trying to make a pogram that prints a random number(without any
pattern( every time we run.
can anyone suggest me the logic
i m seeking anwer for 6 months

Why don't you post one of your recent attempts? Did you remember to call
srand() exactly once?
 
M

Mark McIntyre

Hi Raghu,

sys/types.h is used as it contains the defination of time_t
time_t is the return type of time() function.

This is incorrect - you should include time.h for this.

Also, to the OP, there#s some discussion of this in the FAQ.
Mark McIntyre
 
A

ash

thankx dude,
i really forget about random function, can u tell me how it works?
thankx anyway
ash
 
J

Jordan Abel

But return values from rand() definitly "have a pattern" in most
implementations. (OP specificly asked about a random number generator
that "does not have a pattern").

Using the higher-order bits may or may not mitigate that. I've heard
that it does on the PRNG provided as an example in the standard
True random functions are impossible without support from special
hardware,

I suppose a keyboard would be considered "special hardware" from a
standard C point of view. [that is one of the sources of entropy
commonly used for /dev/random on linux AFAIK - timings, not data, of
course]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top