-
how do i do random numbers i got an example but its not what i wanted
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void main( void )
{
int i;
/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
printf( " %6d\n", rand() );
}
if you test it it will go all over the place and i tried lots of another things to but it just dont work
-
-
like the forumal to make random numbers in c++ , ranges dosent matter please help
-
I still don't understand what you're after - what's "forumal" meant to be? I am guessing it's a spelling mistake or a typo.
If ranges don't matter then what's the problem? rand() returns an integer value between 0 and RAND_MAX, which is 32767.
If you want a number between 0 and 1 or something (I'm guessing what you might want here), then try this:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void main( void )
{
int i, iRandom;
float fRandom0To1;
/* Seed the random-number generator with current time so that
* the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
/* Display 10 numbers. */
for( i = 0; i < 10;i++ )
{ iRandom = rand() % 100; /* resolution of 0.01 */
fRandom0To1 = (float)iRandom / 100; /* float division */
printf( " %f\n", fRandom0To1 );
}
}
-
yeah that was a typo ranges do matter sorry
-
SO no one nows how to make random numbers????? please help some one.... if need somthing that will do it like ramngs in vb thanks.
-
Oh was 'forumal' meant to be 'formula'?
I didn't see this post last time you replied, otherwise I would have responded. rand() returns an integer between 0 and 32767, so assuming you don't need a random integer higher than that, you can just use the mod operator (%) to get a random number in the range you want. For example:
Code:
randomNum0To70 = rand() % 71;
randomNum21To40 = 21 + rand() % 20;
So if you want a general formula:
Code:
/* this expression will return a random number between a and b */
a + rand() % (b - a + 1);
Anyway I think that's what you want. Please, check your messages for typos before you submit your post, it isn't obvious what you mean by 'ramngs' but I'm guess you meant 'ranges'. If you don't make it clear what you need, people won't be able to help you properly. Make sure you give all the information they need. Just saying "this isn't what I want" usually isn't going to get you very far.
Anyway, hope that helps.
-
sorry i was in a hurry, thanks alot man i was working on that all night . thanks alot..
-
i have this in my code, this is all i have .
int nSpace;
nSpace = 21 + rand() % 20;
cout << nSpace << endl;
return 0;
nothing else is that all i need to have or do i still need the time thing when i do that it comes out to do like 22 and i did it like 30 times and it still comes out to be 22 .
-
ok i added the srand and it wokred thanks man
-
To get a random number
int x;
x=rand();
printf("%d\n" x);
-
Umm... yeah that's what the previous 9 posts say.