-
rand() function
Hi, this is my first post...great site...i want to ask sth...i made a small program but i have a problem. when we call rand() we get a number betwen 0 and RAND_MAX. I want to have numbers betwwen 0 and 1. Does anyone know how can i do this? The problem is:
#...
#...
main(){
int x;
srand(5);
x = rand();
x = rand()/RAND_MAX; //output is always 0...
-
I suggest that you read Prelude's article on using rand(). Look out for the term "uniform deviate".
-
1) Do us all a favor and use the code tags.
2) Read about integer division and reflect on your code afterwards.
-
This is how I generate a random number. It usually works for me.
Code:
#include <time.h>
main()
{
int x;
srand(time(NULL));
x = rand() % 2;
}
This sets "x" to a random number between zero and one.
-
your problem is simple....
if you want to get a number between 0 and 1, then you can't declare it as int x;
you should declare it as float x;
-
rand() function
thx v33k....it is working...i can now have numbers bettween 0 and 1...but could you make a program with rand + srand where i can have results like these: (after the . i want only one digit)
e.g.:
output:
0.5
0.6
0.1
0
0
0.8
0.9
0.5
0.6
0.7
0.8
0.4
0.6
i wish you understood what i want...i am working C about 9 months but i miss some simple things..so if sb could help me i would appreciate this...
-
rand() returns an integer between 0 and RAND_MAX. You can use the modulo operator to get a number between 0 and an upper limit, as has already been shown.
0.1 * 10 is an integer, so is 0.9 * 10.
In other words, why not just get an integer between 0 and 10 and then divide it by 10?
-
Floats/doubles are actually bad for 0.1, 0.2, 0.3, because a tenth of a unit is not a "power of two number". If you want fixed precision here, you are better to use an int between 0 and 10, like DeadPlanet says, then when you want to output it as a decimal:
Code:
int x;
printf("%.1f",(float)(x/10));
This should be fine, because printf by default rounds correctly (to nearest, not down). The reason it may have to round is that one tenth is not exactly represented in memory, as just mentioned. For an illustration of that, run this:
Code:
#include <stdio.h>
int main() {
float i;
for (i=0.0f; i<20; i+=0.1f) {
printf("%f\n",i);
}
return 0;
}
The output is not what you are expecting! However, if I used the %.1f template, the numbers would be correct (it will round the imprecision in the representation out).
-
This is easy
use this:
Code:
float result;
result=((float)rand())/RAND_MAX;
now result would contain a random number between 0 and 1
here is a sample program which prints 10 random numbers between 0 and 1:
Code:
#include<stdio.h>
#include<stdlib.h>
void main()
{
float result,i;
for(i=0;i<10;i++)
{
result=((float)rand())/RAND_MAX;
printf("%f\n",result);
}
}
the logic is rand() returns value between 0 and RAND_MAX.. so when we divide by RAND_MAX it'll always give value between 0 and 1(including 0 and 1)
:):)
-
ur problem is that ur getting 0 because u have declared x as an integer.. change it to float because the result which u get is a fraction :):)
-
@sandeep080:
First of all, read the rest of the topic. Your post is a bit... late. But it's even wrong, as rand() returns an integer and RAND_MAX is an integer, meaning that you will divide two integers, and the result will be an integer, not a float.