Public Group

Archived

This topic is now archived and is closed to further replies.

Checking a Random #

This topic is 5621 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Share on other sites
Here,you can use this code to create random numbers:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std ;

int main(){

srand(time(0)) ; // randomizer
// if this is not here, your rand()
// will not produce random numbers
// but instead a repeatable pattern
// time(0) is just the seed
// and since every second gives a different
// time, you''re going to get random
// numbers to work with
// if it doesn''t make sense, it doesn''t
// have to, just know that you MUST
// put srand(time(0)) in your program
// once in order to get random numbers
// when using rand()

// i want a number from 1 to 10
int maximum_desired_number = 10 ;
int my_random_number = rand() % maximum_desired_number + 1 ;
// NOTICE THE +1
// if you do not put the +1 then your random number will begin at zero
// thus if you''ll get numbers in the range of 0 to 9, not 1 to 10!!!!!
return 0 ;
}

Share on other sites
Thanks.
Interesting. Your way definitely is easier, and it works well, truly random.

I created a smaller program to test checking a random number (ie if (my_random_number == 1)...) so that one of the two planets would randomly be chosen. It worked, yet the modified code in my full project gives me the same result each time (in other words it wasn''t random). I suppose this must mean that something somewhere else in my code is wrong and throws of the random choseing somehow. How or what it is that is messing with my randomness I don''t know.
Sigh... more work. Thanks for your help. If and when I do solve this I''ll try to post it.

Share on other sites
No problem.I''m glad i helped you.

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
16
5. 5

• 14
• 29
• 13
• 11
• 11
• Forum Statistics

• Total Topics
631774
• Total Posts
3002288
×