LinuxQuestions.org
Help answer threads with 0 replies.
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-07-2009, 03:50 PM   #1
Guptarulz
LQ Newbie
 
Registered: Oct 2009
Posts: 16
Blog Entries: 1

Rep: Reputation: 1
Random number in C linux


Seems a silly question


I want to generate random number between 0-50
any idea how to get this done in C
 
Old 10-07-2009, 04:21 PM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
#define MAKE_RANDOM(x) (rand() % (((x)+1)&0xFFFFFFF))
int main(){
srand(time(0) / 5000);
printf("%d", MAKE_RANDOM(50));
}
 
Old 10-07-2009, 05:15 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Guptarulz View Post
Seems a silly question


I want to generate random number between 0-50
any idea how to get this done in C
man 3 rand
 
Old 10-08-2009, 02:54 PM   #4
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291
well, if by random you mean pseudo-random then yes using rand() would be the way to go. But, as there is no truly random number (other than maybe radioactive decay), it depends on how random you want it. I recommend you at least seed rand with say an integer from /dev/random or /dev/urandom.

Here's an example of using rand:
http://www.cplusplus.com/reference/c.../cstdlib/rand/

If you want I can post some code using urandom or random.
 
Old 10-08-2009, 03:49 PM   #5
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by smeezekitty View Post
#define MAKE_RANDOM(x) (rand() % (((x)+1)&0xFFFFFFF))
int main(){
srand(time(0) / 5000);
printf("%d", MAKE_RANDOM(50));
}
No, that is not how it's done (I won't catalog all the errors you're making). This is:

Code:
#include <iostream>
#include <cstdlib>

double decentRand(double low = 0, double high = 1)
{
	double x = rand()/((double)RAND_MAX+1);
	return (x * (high-low)) + low;
}
Call it like this:

Code:
double x = decentRand(desired low, desired high);
 
Old 10-08-2009, 04:12 PM   #6
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
works fine for me.
it has no low range.
 
Old 10-08-2009, 04:30 PM   #7
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by smeezekitty View Post
works fine for me.
Yes, on your system, with your word length, and a number of other nonportable assumptions, as well as using the modulo operator to select a subrange, a classic beginner's error.
 
Old 10-08-2009, 04:34 PM   #8
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
well yours returns a floating point for no reason.
that also means it has to do a (potentially slow)floating point to integer conversion.
 
Old 10-08-2009, 04:35 PM   #9
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Code:
long Rand(long low = 0, long high = 1)
{
	long x = rand()/((long)RAND_MAX+1);
	return (x * (high-low)) + low;
}
is better
 
Old 10-08-2009, 04:44 PM   #10
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
No, that's even worse. It'll always return low, except if RAND_MAX+1 overflows.
 
Old 10-08-2009, 04:45 PM   #11
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by smeezekitty View Post
Code:
long Rand(long low = 0, long high = 1)
{
	long x = rand()/((long)RAND_MAX+1);
	return (x * (high-low)) + low;
}
is better
That's really brilliant. You do realize, don't you, that the result of dividing a given integer by a larger integer is always zero?

Have you ever considered testing your code before posting it? Here is a test run of your function's output:

Code:
0
0
0
0
0
0
0
0
0
0
0
0
0
 
Old 10-08-2009, 04:46 PM   #12
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by smeezekitty View Post
well yours returns a floating point for no reason.
that also means it has to do a (potentially slow)floating point to integer conversion.
Yes, and most uses for rand() in modern times require a float or a double. There is an equivalent integer version, but it doesn't see much use these days.
 
Old 10-08-2009, 04:49 PM   #13
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
i have always avoided floating point at all costs (started programming on a 486)
 
Old 10-08-2009, 04:59 PM   #14
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
There *is* a way to get a decent random number out of rand() only using integer ops, but it's painful.

1. Figure out the number of bits B in the next highest power of two of (max - min). If (max -min) happens to already be a power of two, that's nice.
2. Figure out how many meaningful bits rand() can produce. That's relatively easy by counting how many times you have to unsigned right shift to make RAND_MAX zero. Be careful you don't count too many or else entropy goes through the floor.
3. Extract the top B meaningful bits from the rand() call.
4. If that produced a number less than (max - min) you're done. If not, call rand() again until you get a number less than (max - min).

This, of course, assumes that (max - min) < RAND_MAX.

Last edited by tuxdev; 10-08-2009 at 05:01 PM.
 
Old 10-08-2009, 05:22 PM   #15
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Code:
#include <stdlib.h>
#include <time.h>
#include <iostream.h>
long _random(int min, int max){
if(min > max){int j = min; min = max; max = j;}
long r = (long)rand()%(long)((long)max-(long)min);
return min+r;
}
int main(){
srand(time(0) / 2);
cout<<("11 RANDOM NUMBERS FROM 100-150\n");
for(int j = 0;j < 11;j++){
cout<<_random(100, 150) <<'\n';
}
return 0;
}
output:
Code:
11 RANDOM NUMBERS FROM 100-150
102
148
128
107
115
117
131
137
127
120
121
 
  


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
Random number generator for linux based mobile devices usman_minhas Programming 4 05-15-2009 03:05 AM
Random number generation weird behavior in Win && Linux comparation Bassy Programming 8 03-29-2009 09:39 PM
LXer: Safend Finds Weakness in Linux Random Number Generator LXer Syndicated Linux News 0 05-01-2006 02:54 PM
I need random number in C ... purpleburple Programming 4 10-28-2002 04:37 AM
Random number generator for linux Steve_Taylor Programming 12 09-30-2001 04:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:40 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration