![]() |
|
#1
|
|||
|
|||
Random numbersIn C how do u generate a random number between 1 and 6. I know it has something to do with the RND function from stdlib.h but I don't how to use it.
|
|
#2
|
||||
|
||||
|
CPP / C++ / C Code:
|
|
#3
|
||||
|
||||
|
You do have to include stdlib.h as well.
Also, keep in mind that you should only seed the random number function once in your program or you may get unrandom random numbers. There are several posts here that discuss this, but there is nothing really random about the random numbers that this function will give you. When you seed the number generator it selects a predefined list of numbers based on that seed. So if you seed it with the same number, your sequence will always be identical. Cheers, d |
|
#4
|
|||
|
|||
|
Hmm didnt realy work, I need my program to generate 5 different random numbers and assingn them to elements 0 through 4 of an array. When I run the program it always generates the same number for all 5 elements. Is there some way I can generate 5 different random numbers? My program needs to be able to simulate 5 dice and I cant have them all being 1 or 4 or 6.
|
|
#5
|
|||
|
|||
|
put srand(time(NULL)); at the very beginning of your program. and then whenever you need random numbers, just use the rand() function. you must use srand function only once in your program!
__________________
spasms!!! |
|
#6
|
||||
|
||||
|
Quote:
A random number is just a random number there is no guarantee that you will get just a single number in a selection set, in fact it would be pretty rare. What you need to do is to set up a loop that will check the previous numbers to see if they are similar. Something like: CPP / C++ / C Code:
Thats written on the fly (I didn't compile it), but it should give you the idea. HTH, d |
Recent GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [CONTEST?]Data Structure Test | dsmith | C Programming Language | 2 | 06-Jun-2004 16:13 |
| Random() : Make each number onlu appear once | NiXeN | C++ Forum | 3 | 13-Jan-2004 05:47 |
| Need a script for selecting random tabels and from those tabels selecting random ques | mlt | MySQL / PHP Forum | 2 | 12-Sep-2003 10:01 |
| Best way to validate numbers (ids)? | JdS | MySQL / PHP Forum | 1 | 20-Jan-2003 04:55 |
| Random no longer JUST random... | JdS | GIDTopsites™ | 0 | 12-Jan-2003 09:57 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The