ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Just curious. When I created a 5x4 array and filled it with random numbers using the rand() function, I noticed the last number in the row (row 0, column 4) is identical to the first number in the next row (row 1, column 0). Also, each time I run the program, it generates the same numbers when I run the program. Shouldn't the program be generating different numbers?
Originally posted by abk4523 Shouldn't the program be generating different numbers?
Random numbers produced by rand() are pseudorandom actually by applying a function with a given index which is incremented each call. The initial random number is always seeded with 1. The program that paulsm4 provided you demonstrates how the srand() modifies the seed by using the system's clock (which should never be the same thing again). If you want improved numbers, make sure you scale rand()'s return value against RAND_MAX using doubles instead of a pure modulus to have a more distributed result. For example (1-10) from the rand manpage:
If you use just time() to get pseudo random numbers and if you run your program many times in a row, then it's predictable that you will have the same sequences.
It's preferable to use gettimeofday() multiplying tv_sec * tv_usec (microseconds).
Originally posted by abk4523
I noticed the last number in the row (row 0, column 4) is identical to the first number in the next row (row 1, column 0).
Sounds like a mistake in your design/code. I've never seen it happen that the same number is returned after just a couple of calls to rand.
hello guys.... just asking why do we need to write int before main () cause if i don't dev c++ finds an error...
Are you from an C background? In C, any function with no return type specified is assumed to hate a return type of int - in C++, no such assumption is made, and it's a compile-time error to not specify the return value of a function.
If you use just time() to get pseudo random numbers and if you run your program many times in a row, then it's predictable that you will have the same sequences.
It's preferable to use gettimeofday() multiplying tv_sec * tv_usec (microseconds).
Whatever you use, it's also a good idea to "exclusive OR" that value with your process ID. This covers the situation where you run your program many times in rapid succession.
Are you from an C background? In C, any function with no return type specified is assumed to hate a return type of int - in C++, no such assumption is made, and it's a compile-time error to not specify the return value of a function.
John G
ye i am trying to write a program in c that checks if 3000 random numbers are powerful and if it finds 10 for expmple to print the %... and i am using dev c++ for writing the program ? :S but in university we don't use int or something else before the main () .. correct me if i am wrong
There is nothing to correct rel0aded. As JohnGraham told you, if you omit the type the compiler will automatically expect an integer to be returned. It is just a good practice to state what type you expect a function to return, thus (most) people declare main as returning int...
There is nothing to correct rel0aded. As JohnGraham told you, if you omit the type the compiler will automatically expect an integer to be returned. It is just a good practice to state what type you expect a function to return, thus (most) people declare main as returning int...
ok i did
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define COMPUTATIONS 3000
main ()
{int i;
srand(time(NULL));
for (i=0 ; i < COMPUTATIONS ; i++)
{
printf ("random_number[%d]= %d\n",i, rand ());
}
}
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Rep:
Quote:
Originally Posted by wje_lq
Whatever you use, it's also a good idea to "exclusive OR" that value with your process ID. This covers the situation where you run your program many times in rapid succession.
who is going to execute the program !1000000! times per second?
i think that is overboard.
who is going to execute the program !1000000! times per second?
i think that is overboard.
The inclusion of the pid into the calculation is cheap.
There is a place in hell for programmers who tell themselves, "I won't add this fast, cheap wrinkle to my program because I can't imagine someone ever wanting to do XXX with my program." People come up with new uses for programs, uses which the original designers of the programs often can't possibly imagine.
If there is a security aspect to the program (and it need not be cloak and dagger, it could be as simple as generating moves for some sort of game), then it might not be the legitimate user of the program who does something weird; it could be a hostile user. As soon as you bring hostile users into the mix, you can rely on them pushing the envelope in hitherto unimagined ways.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
Rep:
how can you even call a program 1000000 times per second?
bash, batch and C system and exec calls are all too slow.
the only way you could do it is if its from withen it self and in that case it would have the same PID.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.