C++ Random Numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Closed Thread

Join Date: Aug 2006
Posts: 13
Reputation: uu666 is an unknown quantity at this point 
Solved Threads: 0
uu666 uu666 is offline Offline
Newbie Poster

Re: C++ Random Numbers

 
0
  #21
Nov 2nd, 2006
i found something these days that looks like this:

  1. #include <iostream.h> //for cout
  2. #include <conio.h> //for clrscr() and getch()
  3. #include <stdlib.h> //for randomize() and rand()
  4.  
  5. void main()
  6. {
  7. clrscr();
  8.  
  9. randomize();
  10. cout << rand() << endl;
  11.  
  12. getch();
  13. }

hope it helps
Quick reply to this message  
Join Date: Feb 2005
Posts: 84
Reputation: evilsilver is an unknown quantity at this point 
Solved Threads: 1
evilsilver's Avatar
evilsilver evilsilver is offline Offline
Junior Poster in Training

Re: C++ Random Numbers

 
0
  #22
Nov 4th, 2006
Originally Posted by brahle View Post
BTW, why are you people using halfly C and halfly C++?
Why do you use cin and cout and normal pointers? You see, cin and cout are much slower than scanf() and printf(), and when you have to input/output more than 10k of data, you see the difference. That is why I use scanf() and printf(). Normal pointers aren't used much in C++ because there is a templated conatiner vector that you can use to easily manipulate arrays.

i know that is a faster way of doing it but remember that not everyone is dealing with that much memory need and may only know about cout and cin which are simpler to use and are first thing that people learn about (hello world program always uses this, that i see anyways)

this way the code snipet can be used by new programmers and by those that may not know this code yet


just thought i would point that out lol

o and by the way i am using microsoft cisual studio 2005 now, works great and has a lot more functionality than borland, but borland was good to learn on from it's simplicity too lol kk well there wasn't much help in this post so i gonna shut up now lol
Quick reply to this message  
Join Date: Nov 2006
Posts: 7
Reputation: bmanoman is an unknown quantity at this point 
Solved Threads: 0
bmanoman bmanoman is offline Offline
Newbie Poster

Re: C++ Random Numbers

 
0
  #23
Nov 19th, 2006
How would i create more than one random number, between 1, and 3, kind of like the slots [#],[#],[#] each filled with a random number...i tried using the examples here but they would just give me the same random numbers and would bring up the same numbers every time i ran it. if i got [3],[2],[3]. it would keep repeating those every time i ran it...id really appreciate the help
Quick reply to this message  
Join Date: Feb 2005
Posts: 84
Reputation: evilsilver is an unknown quantity at this point 
Solved Threads: 1
evilsilver's Avatar
evilsilver evilsilver is offline Offline
Junior Poster in Training

Re: C++ Random Numbers

 
0
  #24
Nov 21st, 2006
Originally Posted by bmanoman View Post
How would i create more than one random number, between 1, and 3, kind of like the slots [#],[#],[#] each filled with a random number...i tried using the examples here but they would just give me the same random numbers and would bring up the same numbers every time i ran it. if i got [3],[2],[3]. it would keep repeating those every time i ran it...id really appreciate the help


the problem with rand is that it isn't truely random, it is a psuedo random, or fake random. it has a list of numbers that it follows and though is alright for some very light things, is very predictable and will repeat it's self eventually.

if you read the rest of the snipet look for srand() it basically has the compiler pick a spot in the string of numbers it has for psuedo random so that it looks more random everytime you run the program
Quick reply to this message  
Join Date: Dec 2006
Posts: 1
Reputation: draclich is an unknown quantity at this point 
Solved Threads: 0
draclich draclich is offline Offline
Newbie Poster

Re: C++ Random Numbers

 
0
  #25
Dec 14th, 2006
Originally Posted by Bob View Post
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. srand((unsigned)time(0));
  10. int random_integer;
  11. int lowest=1, highest=10;
  12. int range=(highest-lowest)+1;
  13. for(int index=0; index<20; index++){
  14. random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
  15. cout << random_integer << endl;
  16. }
  17. }
Greetings
I'm new to C++ and started playing with the above code. I edited the code a bit to allow for a wider range, and a smaller range, but no matter what range i specified my first number was always a 1 or a 2. Is there a way around that, or is it really random and the 20 some times I ran the code just kept giving me the same result out of bad luck? I'm using Microsoft Visual C++.

Thanks
-Drac
Quick reply to this message  
Join Date: Oct 2006
Posts: 143
Reputation: venomlash is an unknown quantity at this point 
Solved Threads: 2
venomlash's Avatar
venomlash venomlash is offline Offline
Junior Poster

Re: C++ Random Numbers

 
0
  #26
Dec 14th, 2006
Like evilsilver said, rand isn't really random. It uses a really complex algorithm on the seed number (which you set with srand), so every time the program is run, it gives you the same output from the same algorithm with the same input. Try putting a srand([something]) inside the for loop and try again.
Beware of the Rancor. I'm not kidding.
If it doesn't compile, try saying "By the power of MegaMan!!!" <this has kinda worked for me, actually...>
Scotland is NOT North Britain, Glasgow does NOT rhyme with "cow", and Robbie Burns is...well, if you don't already know who he was, you're kinda screwed.
Quick reply to this message  
Join Date: Jan 2007
Posts: 1
Reputation: Xakil is an unknown quantity at this point 
Solved Threads: 0
Xakil's Avatar
Xakil Xakil is offline Offline
Newbie Poster

Re: C++ Random Numbers

 
0
  #27
Jan 27th, 2007
Hi, im kinda new at C++ and this site and I wanted to see if someone could better explain about having a list of numbers (say 50) and then grabing them one by one randomly, but not reusing any of the 50 numbers that have already been used.

Thanks in advanced...
Quick reply to this message  
Join Date: Oct 2006
Posts: 143
Reputation: venomlash is an unknown quantity at this point 
Solved Threads: 2
venomlash's Avatar
venomlash venomlash is offline Offline
Junior Poster

Re: C++ Random Numbers

 
0
  #28
Jan 29th, 2007
Create a
  1. bool list[50]
, an array of 50 boolean (yes/no) values and set all of them to true at the start by using a for loop. Then, create the following function:
  1. int randnum(int num){
  2. return rand()*num/(RAND_MAX+1);
  3. }
Then, inside the body of your program, write the following:
  1. int printnum;
  2. for(int i=0; i<50; i++){
  3. printnum=randnum(50);
  4. if(list[printnum]==true){
  5. list[printnum]=false;
  6. cout<<printnum+1<<endl;
  7. } else {
  8. i--;
  9. }
  10. }
If this doesn't work, tell me.
Venomlash
Beware of the Rancor. I'm not kidding.
If it doesn't compile, try saying "By the power of MegaMan!!!" <this has kinda worked for me, actually...>
Scotland is NOT North Britain, Glasgow does NOT rhyme with "cow", and Robbie Burns is...well, if you don't already know who he was, you're kinda screwed.
Quick reply to this message  
Join Date: Apr 2007
Posts: 1
Reputation: ahlongxp is an unknown quantity at this point 
Solved Threads: 0
ahlongxp ahlongxp is offline Offline
Newbie Poster

Re: C++ Random Numbers

 
0
  #29
Apr 23rd, 2007
Originally Posted by Bob View Post

  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. srand((unsigned)time(0));
  10. int random_integer;
  11. int lowest=1, highest=10;
  12. int range=(highest-lowest)+1;
  13. for(int index=0; index<20; index++){
  14. random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
  15. cout << random_integer << endl;
  16. }
  17. }
  1. int(range*rand()/(RAND_MAX + 1.0));
always return 0 on g++ (GCC) 4.1.2.
changed into
  1. int((double)range*rand()/(double)(RAND_MAX + 1.0)) ;
fixed this problem.
Quick reply to this message  
Join Date: Jul 2007
Posts: 47
Reputation: Arctic wolf is an unknown quantity at this point 
Solved Threads: 1
Arctic wolf Arctic wolf is offline Offline
Light Poster

Re: C++ Random Numbers

 
0
  #30
Jul 21st, 2007
Hello everybody,
I wanted to ask Bob about this piece of code:
  1. int main()
  2. {
  3. srand((unsigned)time(0));
  4. int random_integer;
  5. int lowest=1, highest=10;
  6. int range=(highest-lowest)+1;
  7. for(int index=0; index<20; index++){
  8. random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0));
  9. cout << random_integer << endl;
  10. }
you said that this formula:
  1. random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0))
is better than using modulu because it solves some kind of compiler problems,I wanted to ask what kind of problems does it solve(why is it so different from the one that uses modulu)
and I also wanted to ask how did you develope this formula(what concept is it based on)?

Thank you.
Quick reply to this message  
Closed Thread

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC