Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
10-30-2005, 12:18 PM
|
#1
|
|
Member
Registered: Jun 2004
Distribution: RH8/Fedora Core 4
Posts: 107
Rep:
|
generating random numbers in C
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?
|
|
|
|
10-30-2005, 12:34 PM
|
#2
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
Hmmm - "srand()/rand()" isn't exactly the most sophisticated random number generator in the world, but it *should* work for your purposes.
Please compare what you're doing with this:
Code:
/*
* rand: Generates 5 numbers using standard "srand()/rand()" function
*
* SAMPLE OUTPUT:
* rand[0]= 824522256
* rand[1]= 1360907941
* rand[2]= 1513675795
* rand[3]= 1046462087
* rand[4]= 253823980
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int
main (int argc, char *argv[])
{
/* Simple "srand()" seed: just use "time()" */
unsigned int iseed = (unsigned int)time(NULL);
srand (iseed);
/* Now generate 5 pseudo-random numbers */
int i;
for (i=0; i<5; i++)
{
printf ("rand[%d]= %u\n",
i, rand ());
}
return 0;
}
Your .. PSM
|
|
|
|
10-30-2005, 01:39 PM
|
#3
|
|
Member
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66
Rep:
|
Re: generating random numbers in C
Quote:
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:
Code:
j = 1 + (int)( 10.0 * rand() / ( RAND_MAX + 1.0 ) );
As a side note, having repeatable sequence of pseudo-random numbers helps debugging programs that use random numbers by saving the seed used.
|
|
|
|
10-30-2005, 05:49 PM
|
#4
|
|
Member
Registered: Jun 2005
Posts: 542
Rep:
|
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).
|
|
|
|
10-30-2005, 11:12 PM
|
#5
|
|
Senior Member
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,780
|
Re: generating random numbers in C
Quote:
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.
|
|
|
|
11-05-2009, 04:11 AM
|
#6
|
|
LQ Newbie
Registered: Nov 2009
Location: Greece
Posts: 3
Rep:
|
Quote:
Originally Posted by paulsm4
Hmmm - "srand()/rand()" isn't exactly the most sophisticated random number generator in the world, but it *should* work for your purposes.
Please compare what you're doing with this:
Code:
/*
* rand: Generates 5 numbers using standard "srand()/rand()" function
*
* SAMPLE OUTPUT:
* rand[0]= 824522256
* rand[1]= 1360907941
* rand[2]= 1513675795
* rand[3]= 1046462087
* rand[4]= 253823980
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
--------->int <---------
main (int argc, char *argv[])
{
/* Simple "srand()" seed: just use "time()" */
unsigned int iseed = (unsigned int)time(NULL);
srand (iseed);
/* Now generate 5 pseudo-random numbers */
int i;
for (i=0; i<5; i++)
{
printf ("rand[%d]= %u\n",
i, rand ());
}
return 0;
}
Your .. PSM
|
hello guys.... just asking why do we need to write int before main () cause if i don't dev c++ finds an error...
|
|
|
|
11-05-2009, 04:16 AM
|
#7
|
|
Member
Registered: Oct 2009
Posts: 451
Rep: 
|
Quote:
Originally Posted by rel0aded
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.
John G
|
|
|
|
11-05-2009, 05:03 AM
|
#8
|
|
Member
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 806
Rep: 
|
Quote:
Originally Posted by primo
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.
|
|
|
|
11-05-2009, 07:55 AM
|
#9
|
|
Senior Member
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 4,680
|
Quote:
Originally Posted by abk4523
I created a 5x4 array
|
Quote:
|
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).
|
If there are use 4 columns, row 0 column 4 is the same memory location as row 1 column 0. That is how arrays are stored.
4 columns means columns 0 .. 3. There is no column 4.
If you think your 5x4 was 5 columns and 4 rows, I think you are mistaken. Your symptoms say you have 4 columns.
The fact that you are using the array for random numbers just distracted you from an issue that is in the structure of the array itself.
|
|
|
|
11-05-2009, 08:04 AM
|
#10
|
|
LQ Newbie
Registered: Nov 2009
Location: Greece
Posts: 3
Rep:
|
Quote:
Originally Posted by JohnGraham
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 
|
|
|
|
11-05-2009, 08:20 AM
|
#11
|
|
LQ Newbie
Registered: Sep 2009
Posts: 5
Rep:
|
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...
|
|
|
|
11-05-2009, 08:47 AM
|
#12
|
|
LQ Newbie
Registered: Nov 2009
Location: Greece
Posts: 3
Rep:
|
Quote:
Originally Posted by timepasser
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 ());
}
}
without writing int before main ()
and worked 
|
|
|
|
11-05-2009, 07:37 PM
|
#13
|
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,136
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.
|
|
|
|
11-05-2009, 08:16 PM
|
#14
|
|
Member
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 806
Rep: 
|
Quote:
Originally Posted by smeezekitty
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.
|
|
|
|
11-05-2009, 08:45 PM
|
#15
|
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,136
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:47 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|