473,396 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

really terrible

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
22 1570
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
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
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
"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
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
"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
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
"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
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
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
"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
"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
"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
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
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
"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
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
ash
thankx dude,
i really forget about random function, can u tell me how it works?
thankx anyway
ash

Dec 30 '05 #19
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Zam | last post by:
Hello World, Windows 2000 Server. IIS 5.0 Previous message/issue was: "sending HUGE file to client via Response.BinaryWrite" It's solved now. But now I have other unsolved and terrible thing:...
5
by: Pieter | last post by:
Hi, I'm getting a really terrible and anoying bug/problem in VS.NET 2005: 1. Create a new Windows Application. 2. Add a new class Class1. 3. Add a usercontrol UserControl1. 4. Add a public...
7
by: forgroupsonly | last post by:
Hello All. I wonder if browsers developers scoff at CSS developers... I do simple tests while reading CSS2.1 specification, just few boxes. And from time to time I see that recent browsers...
50
by: John Salerno | last post by:
I know it's popular and very handy, but I'm curious if there are purists out there who think that using something like: for x in range(10): #do something 10 times is unPythonic. The reason I...
0
by: raylopez99 | last post by:
I ran afoul of this Compiler error CS1612 recently, when trying to modify a Point, which I had made have a property. It's pointless to do this (initially it will compile, but you'll run into...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.