PDA

Click to See Complete Forum and Search --> : RANDOM NUMBER


BoB
Jan 12th, 2001, 10:24 AM
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

HarryW
Jan 12th, 2001, 10:30 AM
Well what did you want?

BoB
Jan 12th, 2001, 10:51 AM
like the forumal to make random numbers in c++ , ranges dosent matter please help

HarryW
Jan 12th, 2001, 11:47 AM
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:


#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 );
}
}

BoB
Jan 13th, 2001, 07:13 AM
yeah that was a typo ranges do matter sorry

BoB
Jan 25th, 2001, 06:43 PM
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.

HarryW
Jan 25th, 2001, 07:00 PM
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:



randomNum0To70 = rand() % 71;

randomNum21To40 = 21 + rand() % 20;



So if you want a general formula:


/* 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.

BoB
Jan 25th, 2001, 07:38 PM
sorry i was in a hurry, thanks alot man i was working on that all night . thanks alot..

BoB
Jan 26th, 2001, 03:20 PM
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 .

BoB
Jan 26th, 2001, 03:24 PM
ok i added the srand and it wokred thanks man

MPrestonf12
Jan 29th, 2001, 04:58 PM
To get a random number

int x;
x=rand();
printf("%d\n" x);

HarryW
Jan 29th, 2001, 11:20 PM
Umm... yeah that's what the previous 9 posts say.