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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
|
10-07-2009, 03:50 PM
|
#1
|
LQ Newbie
Registered: Oct 2009
Posts: 16
Rep:
|
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
|
|
|
10-07-2009, 04:21 PM
|
#2
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
|
#define MAKE_RANDOM(x) (rand() % (((x)+1)&0xFFFFFFF))
int main(){
srand(time(0) / 5000);
printf("%d", MAKE_RANDOM(50));
}
|
|
|
10-07-2009, 05:15 PM
|
#3
|
Senior Member
Registered: May 2005
Posts: 4,481
|
Quote:
Originally Posted by Guptarulz
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
|
|
|
10-08-2009, 02:54 PM
|
#4
|
LQ Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
|
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.
|
|
|
10-08-2009, 03:49 PM
|
#5
|
Member
Registered: Sep 2009
Distribution: Fedora
Posts: 835
Rep: 
|
Quote:
Originally Posted by smeezekitty
#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);
|
|
|
10-08-2009, 04:12 PM
|
#6
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
|
works fine for me.
it has no low range.
|
|
|
10-08-2009, 04:30 PM
|
#7
|
Member
Registered: Sep 2009
Distribution: Fedora
Posts: 835
Rep: 
|
Quote:
Originally Posted by smeezekitty
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.
|
|
|
10-08-2009, 04:34 PM
|
#8
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
|
well yours returns a floating point for no reason.
that also means it has to do a (potentially slow)floating point to integer conversion.
|
|
|
10-08-2009, 04:35 PM
|
#9
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
|
Code:
long Rand(long low = 0, long high = 1)
{
long x = rand()/((long)RAND_MAX+1);
return (x * (high-low)) + low;
}
is better
|
|
|
10-08-2009, 04:44 PM
|
#10
|
Senior Member
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012
Rep: 
|
No, that's even worse. It'll always return low, except if RAND_MAX+1 overflows.
|
|
|
10-08-2009, 04:45 PM
|
#11
|
Member
Registered: Sep 2009
Distribution: Fedora
Posts: 835
Rep: 
|
Quote:
Originally Posted by smeezekitty
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
|
|
|
10-08-2009, 04:46 PM
|
#12
|
Member
Registered: Sep 2009
Distribution: Fedora
Posts: 835
Rep: 
|
Quote:
Originally Posted by smeezekitty
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.
|
|
|
10-08-2009, 04:49 PM
|
#13
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
|
i have always avoided floating point at all costs (started programming on a 486)
|
|
|
10-08-2009, 04:59 PM
|
#14
|
Senior Member
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012
Rep: 
|
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.
|
|
|
10-08-2009, 05:22 PM
|
#15
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339
|
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
|
|
|
All times are GMT -5. The time now is 01:21 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|