LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 09-12-2001, 04:45 PM   #1
Steve_Taylor
LQ Newbie
 
Registered: Sep 2001
Posts: 22

Rep: Reputation: 15
Smile Random number generator for linux


i have a Random number generatorfor win32 but when i compiled it and ran it on linux it always gave 0 as the generated number but on windows it gives a differnt number!?
can anyone help?


Code:
int num;
srand(time(NULL));
num=(rand()*100)/(RAND_MAX+1);

printf(" rand == %d ",num);
 
Old 09-12-2001, 07:09 PM   #2
kin
LQ Newbie
 
Registered: Sep 2001
Posts: 2

Rep: Reputation: 0
hi

try to assign and print out as a double or float and use %f instead of %d, it might help.
 
Old 09-13-2001, 03:17 PM   #3
Steve_Taylor
LQ Newbie
 
Registered: Sep 2001
Posts: 22

Original Poster
Rep: Reputation: 15
i tred that it still did not make a differnce

does anyone know of any good randum number generators that if givvena number say 37 would generate a number between 1 and 37??
 
Old 09-23-2001, 04:44 PM   #4
gluon
Member
 
Registered: Jun 2001
Location: Switzerland
Distribution: SuSE 8.[012], Gentoo
Posts: 42

Rep: Reputation: 15
hi


the main problem of your prog is that you have an overflow!!! Just try something like :





int num;


srand(time(NULL));


return (int) (((double) rand() / (double) RAND_MAX) * (double) limit) + 1;





by

 
Old 09-23-2001, 08:39 PM   #5
habiblove
Member
 
Registered: Feb 2001
Location: Smyrna, TN
Distribution: Kubuntu 6.1, Ubuntu 7.04 Server, Knoppix
Posts: 59

Rep: Reputation: 15
Anything wrong with using this?



srand(time(NULL));
array[i] = rand() % 1001;
 
Old 09-23-2001, 08:44 PM   #6
habiblove
Member
 
Registered: Feb 2001
Location: Smyrna, TN
Distribution: Kubuntu 6.1, Ubuntu 7.04 Server, Knoppix
Posts: 59

Rep: Reputation: 15
Sorry about previous post. I am adapting this from previous code.

Anything wrong with using this?


int num;
srand(time(NULL));
num = rand() % 38;
return num;
 
Old 09-23-2001, 09:59 PM   #7
habiblove
Member
 
Registered: Feb 2001
Location: Smyrna, TN
Distribution: Kubuntu 6.1, Ubuntu 7.04 Server, Knoppix
Posts: 59

Rep: Reputation: 15
Or if you wanted to create a random number from 1 to limit, try this:

int num;
int limit;

srand(time(NULL));

num = (rand() % limit) + 1;

return num;
 
Old 09-24-2001, 02:54 PM   #8
Steve_Taylor
LQ Newbie
 
Registered: Sep 2001
Posts: 22

Original Poster
Rep: Reputation: 15
Dont mean to sound stupid but what does the % do i have looked it up in the book that i have and cant find it? well not used in this way.
 
Old 09-24-2001, 04:02 PM   #9
habiblove
Member
 
Registered: Feb 2001
Location: Smyrna, TN
Distribution: Kubuntu 6.1, Ubuntu 7.04 Server, Knoppix
Posts: 59

Rep: Reputation: 15
The % is called the modulus. It gives you the remainder of dividing two numbers.

Thus, 13 % 10 returns 3. Using this is nice because:

( number % limit ) + 1 always returns a number from 1 through the limit.
 
Old 09-25-2001, 04:20 AM   #10
gluon
Member
 
Registered: Jun 2001
Location: Switzerland
Distribution: SuSE 8.[012], Gentoo
Posts: 42

Rep: Reputation: 15
OK habiblove,

you're right : modulus seems to be much easier... sorry !


 
Old 09-25-2001, 06:41 PM   #11
habiblove
Member
 
Registered: Feb 2001
Location: Smyrna, TN
Distribution: Kubuntu 6.1, Ubuntu 7.04 Server, Knoppix
Posts: 59

Rep: Reputation: 15
It is pretty neat. Who said a college education isn't worth anything?
 
Old 09-26-2001, 12:39 PM   #12
Steve_Taylor
LQ Newbie
 
Registered: Sep 2001
Posts: 22

Original Poster
Rep: Reputation: 15
i went to college but din't get all that much out of it
 
Old 09-30-2001, 04:21 PM   #13
Colonel Panic
Member
 
Registered: Jul 2001
Location: California
Distribution: Red Hat 7.1, Slackware 8.0
Posts: 216

Rep: Reputation: 30
main()
{
printf("I have no idea!\n")
}

*****Colonel Panic*****
 
  


Reply


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
does GNU's g77 come with a random number generator? terrence Programming 1 08-30-2005 09:29 PM
Number series generator sniff Programming 8 08-23-2005 03:26 AM
Python Random number sethgeekx86 Programming 4 06-10-2004 08:55 AM
Help: Need random password generator linuxgamer Linux - Software 5 12-02-2003 01:01 PM
I need random number in C ... purpleburple Programming 4 10-28-2002 04:37 AM


All times are GMT -5. The time now is 09:07 PM.

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