By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,381 Members | 1,640 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,381 IT Pros & Developers. It's quick & easy.

really terrible

P: n/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

Dec 29 '05 #1
Share this Question
Share on Google+
22 Replies


P: n/a
ash wrote:
i m trying to make a pogram that prints a random number(without any
pattern( every time we run.


Take a look at random() function in stdlib.h.

--
Madhav.

Dec 29 '05 #2

P: n/a
On 28 Dec 2005 21:35:10 -0800, "Madhav" <ma***********@gmail.com>
wrote in comp.lang.c:
ash wrote:
i m trying to make a pogram that prints a random number(without any
pattern( every time we run.


Take a look at random() function in stdlib.h.


There is no standard C random() function, certainly not in stdlib.h.
Perhaps you meant rand()?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 29 '05 #3

P: n/a
Jack Klein wrote:


There is no standard C random() function, certainly not in stdlib.h.
Perhaps you meant rand()?


Yeah, Sorry for that.
--
Madhav.

Dec 29 '05 #4

P: n/a
"Madhav" <ma***********@gmail.com> writes:
ash wrote:
i m trying to make a pogram that prints a random number(without any
pattern( every time we run.


Take a look at random() function in stdlib.h.


There is no random() function in stdlib.h. Are you thinking of rand()?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 29 '05 #5

P: n/a
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

Dec 29 '05 #6

P: n/a
"Rahul Chandok" <ch*******@gmail.com> writes:
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?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 29 '05 #7

P: n/a
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

Dec 29 '05 #8

P: n/a
"Rahul Chandok" <ch*******@gmail.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 29 '05 #9

P: n/a
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

Dec 29 '05 #10

P: n/a
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

Dec 29 '05 #11

P: n/a
"Rahul Chandok" <ch*******@gmail.com> wrote:

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, it doesn't. In C, time_t is defined in <time.h>.
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
Dec 29 '05 #12

P: n/a
"Madhav" <ma***********@gmail.com> writes:
Jack Klein wrote:


There is no standard C random() function, certainly not in stdlib.h.
Perhaps you meant rand()?


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

Dec 29 '05 #13

P: n/a
"ash" <as************@rediffmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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.

Dec 29 '05 #14

P: n/a
Rahul Chandok wrote:

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/>
Dec 29 '05 #15

P: n/a
Niklas Norrthon <do********@invalid.net> wrote:
"Madhav" <ma***********@gmail.com> writes:
Jack Klein wrote:
There is no standard C random() function, certainly not in stdlib.h.
Perhaps you meant rand()?


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.


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
Dec 29 '05 #16

P: n/a
"ash" wrote:
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?
Dec 29 '05 #17

P: n/a
On 29 Dec 2005 01:50:11 -0800, in comp.lang.c , "Rahul Chandok"
<ch*******@gmail.com> wrote:
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
--

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dec 30 '05 #18

P: n/a
ash
thankx dude,
i really forget about random function, can u tell me how it works?
thankx anyway
ash

Dec 30 '05 #19

P: n/a
On 2005-12-29, Niklas Norrthon <do********@invalid.net> wrote:
"Madhav" <ma***********@gmail.com> writes:
Jack Klein wrote:

>
> There is no standard C random() function, certainly not in stdlib.h.
> Perhaps you meant rand()?
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").


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]
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

Dec 30 '05 #20

P: n/a
On 2005-12-29, Keith Thompson <ks***@mib.org> wrote:
int main()


Ok, but "int main(void)" is better.


This came up recently in ##c - As far as I can tell from reading either
standard, they mean the same thing in a function declarator that is part
of a function definition [only differing when it is not part of the
function definition]

[now, that's not to say that it's not labeled "obsolescent" by the
standard - i suspect that if it ever is _really_ deprecated, though, it
will be in favor of making it always mean (void), rather than making it
a syntax error.]
Dec 30 '05 #21

P: n/a
On 2005-12-29, Rahul Chandok <ch*******@gmail.com> wrote:
Hi Raghu,

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


time.h also includes that typedef.
Dec 30 '05 #22

P: n/a
"ash" <as************@rediffmail.com> writes:
thankx dude,
i really forget about random function, can u tell me how it works?
thankx anyway
ash


You haven't been following this newsgroup, have you?

If you expect us to be able to read what you write, use proper
capitalization and avoid silly abbreviations like "u" for "you".

Read <http://cfaj.freeshell.org/google/> and follow its advice.

There is no standard C function called "random". There is a rand()
function. To find out how it works, read your system's documentation.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 30 '05 #23

This discussion thread is closed

Replies have been disabled for this discussion.