|
-
Jan 12th, 2001, 11:24 AM
#1
Thread Starter
Addicted Member
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
WHat would we do with out Microsoft.
A lot more.
-
Jan 12th, 2001, 11:30 AM
#2
Frenzied Member
Harry.
"From one thing, know ten thousand things."
-
Jan 12th, 2001, 11:51 AM
#3
Thread Starter
Addicted Member
like the forumal to make random numbers in c++ , ranges dosent matter please help
WHat would we do with out Microsoft.
A lot more.
-
Jan 12th, 2001, 12:47 PM
#4
Frenzied Member
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 );
}
}
Harry.
"From one thing, know ten thousand things."
-
Jan 13th, 2001, 08:13 AM
#5
Thread Starter
Addicted Member
yeah that was a typo ranges do matter sorry
WHat would we do with out Microsoft.
A lot more.
-
Jan 25th, 2001, 07:43 PM
#6
Thread Starter
Addicted Member
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.
WHat would we do with out Microsoft.
A lot more.
-
Jan 25th, 2001, 08:00 PM
#7
Frenzied Member
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.
Harry.
"From one thing, know ten thousand things."
-
Jan 25th, 2001, 08:38 PM
#8
Thread Starter
Addicted Member
sorry i was in a hurry, thanks alot man i was working on that all night . thanks alot..
WHat would we do with out Microsoft.
A lot more.
-
Jan 26th, 2001, 04:20 PM
#9
Thread Starter
Addicted Member
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 .
WHat would we do with out Microsoft.
A lot more.
-
Jan 26th, 2001, 04:24 PM
#10
Thread Starter
Addicted Member
ok i added the srand and it wokred thanks man
WHat would we do with out Microsoft.
A lot more.
-
Jan 29th, 2001, 05:58 PM
#11
Hyperactive Member
To get a random number
int x;
x=rand();
printf("%d\n" x);
Matt 
-
Jan 30th, 2001, 12:20 AM
#12
Frenzied Member
Umm... yeah that's what the previous 9 posts say.
Harry.
"From one thing, know ten thousand things."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|