Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 11-05-2009, 08:52 PM   #16
Senior Member
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115

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

Rep: Reputation: 47
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

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

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, 02:31 AM   #18
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, 02: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
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, 07:48 AM   #20
LQ Guru
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

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

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, 04:08 PM   #21
Registered: Aug 2008
Distribution: Gentoo; VoidLinux; Funtoo
Posts: 216

Rep: Reputation: 18
need help

this random number program isnt working anyone know what im doing wrong?
#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);
   return 0;

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


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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:12 PM.

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