![]() |
|
#1
|
|||
|
|||
Randomness but in a good wayI was wondering if there was anyway of using srand to create a number within given boundries, 6-800(or something similar. I had a thought of a way to do it but wasnt sure if once again there was a better way to do what seems like a simpe task. But my thoughts go along the line of:
CPP / C++ / C Code:
This would only work if my knowledge of an integer being between -32768 and 32768 is correct otherwise it gets unbelievably huge, at which point i'd leave it out of my program. I can ignore the negative numbers as I could square then square root the number. |
|
#2
|
||||
|
||||
|
Well, I know this equation will give you a random number between 1 and 4...
num = (rand() % 4) + 1; So... num = (rand() % 800) + 6; ...should give you a number between 6 and 800. __________________
-Aaron |
|
#3
|
||||
|
||||
|
I think it would be more like:
CPP / C++ / C Code:
The modulus portion should produce a number between 0 and 794 (the remainder of integer division by 795 of any number) and then by adding 6 you should get 6 through 800. |
|
#4
|
||||
|
||||
|
Quote:
__________________
-Aaron |
|
#5
|
||||
|
||||
|
That's a clever little formula, I must remember it.
__________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#6
|
||||
|
||||
|
Quote:
Code:
__________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#7
|
|||
|
|||
|
Is there anyway of getting the numbers to be "more random" its just whenever I run the program I get the same random numbers ihe same order eg. the first time I run the program I always get x1=554. Is there a way this would be a "real" random number?
CPP / C++ / C Code:
|
|
#8
|
|||
|
|||
|
Ignore where it says "num" in the code, i simpy forgot to remove it after I first put it into the program.
|
|
#9
|
||||
|
||||
|
Quote:
Hi Warny. In order to make your "random" numbers go in random sequences, you need to used the srand(int) command at the start of your program with a different number. This is usually accomplished by passing the current time to the srand command. Search this forum as this has been well covered previously and there are several good examples. Cheers, d |
|
#10
|
|||
|
|||
|
ok i checked previous posts and also went on to tutorial sites and even tried the example given with my compiler(big mistake). Im still kind of lost on the subject and thought I had it but dont understand entirely how each line interacts. This is just a little program I made to print a random number but as b4 the number returned is always the same, I assume its that I hav'nt linked the time to the rand statement but this is what I don't understand:
CPP / C++ / C Code:
|
Recent GIDBlog
Toyota - 2008 November Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Hey, i need a good upload feature for my site | alex | Web Design Forum | 2 | 11-Feb-2004 11:59 |
| I Hope I'll have a good stay here :) | cityit | New Member Introductions | 2 | 28-Dec-2003 23:49 |
| I need a good wed designer | Kulok | New Member Introductions | 4 | 14-Sep-2003 20:35 |
| A very good sitemap script | jrobbio | MySQL / PHP Forum | 3 | 03-Mar-2003 02:07 |
| A good pop-under script? | JdS | Web Design Forum | 4 | 18-Jun-2002 09:39 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The