LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 11-05-2009, 09:52 PM   #16
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115

640K is enough for anybody.
 
Old 11-06-2009, 01:53 AM   #17
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
Quote:
Originally Posted by smeezekitty View Post
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.
If you XOR the PID, you don't have to use that type of time resolution.

I can't tell you why, but I like

Code:
seed ( getpid() ^ time() );
better than

Code:
seed( tv_sec * tv_usec (microseconds));
Moore's law ought to knock about 3 orders of magnitude off your safety factor there in 10 years, or 6 in 20. The first line of code will work just as well then as now. The second? I'll take your bet that I can't launch processes that fast in 10 years.
 
Old 11-06-2009, 03:31 AM   #18
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Having looked through the thread it would appear that the answer I was going to give to the original problem has already been given by Johnsfine.

However, the digression on how you seed the random numbers raises a few thoughts. To me I don't like the idea of using the PID because first it is not cross platform. I also like to keep a record of the seed so that the process can be recreated so I tend not to use time() either rather a variable that could be read in or derived (possibly from the time function). But basically if having "random numbers" is a critical element of a program then don't use the rand function but look for a means of getting true random numbers.
 
Old 11-06-2009, 03:39 AM   #19
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
Quote:
Having looked through the thread it would appear that the answer I was going to give to the original problem has already been given by Johnsfine.
johnsfine was a little late This thread was started 4 years ago
 
Old 11-06-2009, 08:48 AM   #20
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,138

Rep: Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127Reputation: 1127
Quote:
Originally Posted by Wim Sturkenboom View Post
johnsfine was a little late This thread was started 4 years ago
Oops!

I didn't notice the fact that the thread was reopened with a different question, nor the dates on the earlier posts. I just saw that there seemed to be no answer posted for the original question.

I really wish the forum software would give some very visible indication when there is a giant jump in date between two successive posts in a thread.
 
Old 01-10-2011, 05:08 PM   #21
darthaxul
Member
 
Registered: Aug 2008
Distribution: Gentoo; Funtoo
Posts: 206

Rep: Reputation: 18
need help

this random number program isnt working anyone know what im doing wrong?
Code:
#include <gtk/gtk.h>
#include <glib.h>
 
GRand* g_rand_new (void);
gint number = g_random_int_range (GRand *g_rand_new_with_seed, gint32 3, gint 12);
 
void changer(GtkWidget *widget, gpointer label)
{
 
   sprintf(buf, "%d", number);
   gtk_label_set_text(label, buf);
}
 
int main( int argc, char *argv[])
{
   GtkWidget *window;
   GtkWidget *label;
   GtkWidget *hitButton;
 
   g_signal_connect(hitButton, "clicked", G_CALLBACK(changer), label);
   gtk_main();
   return 0;
}

Last edited by crabboy; 01-12-2011 at 09:47 AM.
 
  


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 11:37 AM
code for generating random samples! aru_04 Programming 2 07-21-2005 09:35 AM
Generating Gaussian Random Numbers R00ts Programming 2 08-11-2004 12:51 AM
Creating random numbers from shell with /dev/random khermans Linux - General 1 07-13-2004 01:12 PM
Generating random numbers for C program J-Stew Programming 14 02-06-2004 05:49 AM


All times are GMT -5. The time now is 08:02 AM.

Main Menu
Advertisement
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