LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 10-30-2005, 12:18 PM   #1
abk4523
Member
 
Registered: Jun 2004
Distribution: RH8/Fedora Core 4
Posts: 110

Rep: Reputation: 15
Question 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?
 
Old 10-30-2005, 12:34 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 10-30-2005, 01:39 PM   #3
naf
Member
 
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66

Rep: Reputation: 15
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.
 
Old 10-30-2005, 05:49 PM   #4
primo
Member
 
Registered: Jun 2005
Posts: 542

Rep: Reputation: 34
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).
 
Old 10-30-2005, 11:12 PM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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.
 
Old 11-05-2009, 04:11 AM   #6
rel0aded
LQ Newbie
 
Registered: Nov 2009
Location: Greece
Posts: 3

Rep: Reputation: 0
Lightbulb

Quote:
Originally Posted by paulsm4 View Post
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...
 
Old 11-05-2009, 04:16 AM   #7
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by rel0aded View Post
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
 
Old 11-05-2009, 05:03 AM   #8
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 808

Rep: Reputation: 178Reputation: 178
Quote:
Originally Posted by primo View Post
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.
 
Old 11-05-2009, 07:55 AM   #9
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,051

Rep: Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100Reputation: 1100
Quote:
Originally Posted by abk4523 View Post
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.
 
Old 11-05-2009, 08:04 AM   #10
rel0aded
LQ Newbie
 
Registered: Nov 2009
Location: Greece
Posts: 3

Rep: Reputation: 0
Quote:
Originally Posted by JohnGraham View Post
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
 
Old 11-05-2009, 08:20 AM   #11
timepasser
LQ Newbie
 
Registered: Sep 2009
Posts: 5

Rep: Reputation: 1
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...
 
Old 11-05-2009, 08:47 AM   #12
rel0aded
LQ Newbie
 
Registered: Nov 2009
Location: Greece
Posts: 3

Rep: Reputation: 0
Exclamation

Quote:
Originally Posted by timepasser View Post
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
 
Old 11-05-2009, 07:37 PM   #13
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,223

Rep: Reputation: 167Reputation: 167
Quote:
Originally Posted by wje_lq View Post
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.
 
Old 11-05-2009, 08:16 PM   #14
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: Debian lenny, Slackware 12
Posts: 808

Rep: Reputation: 178Reputation: 178
Quote:
Originally Posted by smeezekitty View Post
who is going to execute the program !1000000! times per second?
i think that is overboard.
  1. The inclusion of the pid into the calculation is cheap.
  2. 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.
  3. 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.
 
Old 11-05-2009, 08:45 PM   #15
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,223

Rep: Reputation: 167Reputation: 167
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.
 
  


Reply

Tags
programming, random number


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Random numbers in C loke137 Programming 9 09-11-2010 10:37 AM
code for generating random samples! aru_04 Programming 2 07-21-2005 08:35 AM
Generating Gaussian Random Numbers R00ts Programming 2 08-10-2004 11:51 PM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 12:12 PM
Generating random numbers for C program J-Stew Programming 14 02-06-2004 04:49 AM


All times are GMT -5. The time now is 01:30 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration