![]() |
|
#1
|
|||
|
|||
randomnessI'm learning C++ and want to write a program to shuffle a deck of cards. I've done it in 3 other languages, but I dont know any way to do random numbers (yet). so does anyone know a simple way to get a random number, or check the clock so I can use that to create a number or ANYTHING not waay complicated? It would be really nice. If not, i just get to follow the stupid book i have to learn other stuff. (which doesnt have any random/clock commands) thanks!
|
|||
|
#2
|
||||
|
||||
|
Hey ambeco, welcome to the forums!
In another thread, I was helping a guy get random numbers where he was using some random number generators. You can view this thread here. The basic routine that he (jack) had written was: CPP / C++ / C Code:
If you read through that thread though, the biggest problem is that the number is surprisingly unrandom. One thing that I suggested is to put in a sleep command to change the time value if you are going to be taking successive random numbers. Let me know , if this helps or not. Last edited by dsmith : 03-Feb-2004 at 22:05.
|
|
#3
|
|||
|
|||
what about timeI think I can use the seconds on the clock to come up with sufficient randomness. does C++ have a command to take the time like VBASIC does? I dont understand enough C++ yet to understand how that bit that you gave me works. I'll see if I can figure out how to work it though. Thanks for what you already gave me!
BTW, heres what I had in mind: int(sin(seconds)*.5+.5*(max-min))+min |
|
#4
|
|||
|
|||
srandI was experimenting with the bit you gave me, but it kept coming up with errors with the srand and lrand. I searched the link you gave me, but I couldnt find anything about these. I even tried including all of those files and still no good. What are these, and how do I use them? Thanks for anything you can help with!
|
|
#5
|
||||
|
||||
|
Hi ambeco.
Sorry for the mishap with the program. Reading through what you want to do and my limited experience with random number generation, I think you may just want to use the random and srandom functions. You will probably only want to actually seed it one time for each time the program is ran. Try this out & see if it gives you any results. This should basically just print 1000 random numbers to your screen (and they should all be different) CPP / C++ / C Code:
This compiled without a hitch using gcc under linux. This is pretty vanilla c code as far as I know you should be able to compile it anywhere... Let me know if you still have problems. |
|
#6
|
||||
|
||||
|
if u cannot understand the bottom part u can email:taychinghwa@hotmail.com or yahoo messager me:taychinghwa
CPP / C++ / C Code:
Last edited by dsmith : 03-Feb-2004 at 22:02.
Reason: Added syntax highlighting
|
|
#7
|
||||
|
||||
|
Hi Tay. Welcome to the forums!
First of all, thank you for the response! Sometimes it seems that I am the only one responding on this board. It's especially helpful, because I forget that people want C++ syntax There have been several questions about random numbers and it has caused me to research it a bit. What I have figured out is that random numbers are pretty easy to replicate. In order to verify this, I downloaded and ran your routine. First of all, I did a small modification, ala: CPP / C++ / C Code:
Your original code, printed b before anything was in it. (Now that was an original number). Also in order to get a five digit number, you need to % by 10000, not 5. Your original code gave random numbers between 0 and 5. Then I ran it and here is the output Code:
I ran it again and again and again. Every time it output the exact same sequence above. Therefore, that is the reason to seed the random number with the time value as it will produce a different sequence every time you run it. I think that the man page for rand explains it best:
Finally, the difference between the rand() and random() function. On most compilers these are identical. However on some older implementations, rand is different. Once more to quote from the man page for rand:
So, in other words, using rand() is probably okay for 95% of the compilers out there. Once again, I really appreciate your input and feedback. It is discussions like these that make these forums so useful! Last edited by dsmith : 04-Feb-2004 at 08:10.
|
|
#8
|
|||
|
|||
|
Thanks for the codes! dsmith, your code wouldnt compile, came up with several errors(my compiler wont do srandom, just srand, and didnt understand your printf), so what I did was took the part that grabs the time and stuck it into tay's program so that I get a sequence of random numbers, that changes every second. I can now modify this for my needs. Thanks you two! I think I will insert the bare random coding for other newbs to use for whatever simply:
CPP / C++ / C Code:
|
|
#9
|
||||
|
||||
|
sorry i forgot a little bit things
thx for infrom instead of " srand((0)) " change it to srand(time(0)) add a "time" inside the bracket then all the digit will total different #include <iostream> #include <cstdlib> using namespace std; int main () { int b; srand(time(0)); for (int i=0; i<20; i++) { b=rand()%10000; cout<<b<<" "; } cout<<endl<<endl; system("pause"); return 0; } |
|
#10
|
||||
|
||||
|
dsmith is using the c way to code not c++ so u might be confuse it
actually almost is same 1, if u understand his code u should be know how to convert it in c++. if u cannot convert it mean u still not really fully understand it. try to learn it programmer is learn algorithms not syntex, if we know algorithms we can apply in any type of language like c / c++ or java same concept but different syntex :-) Quote:
|
Recent GIDBlog
Problems with the Navy (Enlisted) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The