| | |
C++ Random Numbers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2006
Posts: 13
Reputation:
Solved Threads: 0
i found something these days that looks like this:
hope it helps
C++ Syntax (Toggle Plain Text)
#include <iostream.h> //for cout #include <conio.h> //for clrscr() and getch() #include <stdlib.h> //for randomize() and rand() void main() { clrscr(); randomize(); cout << rand() << endl; getch(); }
hope it helps
•
•
•
•
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
•
•
Join Date: Nov 2006
Posts: 7
Reputation:
Solved Threads: 0
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
•
•
•
•
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
•
•
Join Date: Dec 2006
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { srand((unsigned)time(0)); int random_integer; int lowest=1, highest=10; int range=(highest-lowest)+1; for(int index=0; index<20; index++){ random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); cout << random_integer << endl; } }
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
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.
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.
Create a , 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:
Then, inside the body of your program, write the following:
If this doesn't work, tell me.
Venomlash
C++ Syntax (Toggle Plain Text)
bool list[50]
C++ Syntax (Toggle Plain Text)
int randnum(int num){ return rand()*num/(RAND_MAX+1); }
C++ Syntax (Toggle Plain Text)
int printnum; for(int i=0; i<50; i++){ printnum=randnum(50); if(list[printnum]==true){ list[printnum]=false; cout<<printnum+1<<endl; } else { i--; } }
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.
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.
•
•
Join Date: Apr 2007
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { srand((unsigned)time(0)); int random_integer; int lowest=1, highest=10; int range=(highest-lowest)+1; for(int index=0; index<20; index++){ random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); cout << random_integer << endl; } }
C++ Syntax (Toggle Plain Text)
int(range*rand()/(RAND_MAX + 1.0));
changed into
C++ Syntax (Toggle Plain Text)
int((double)range*rand()/(double)(RAND_MAX + 1.0)) ;
•
•
Join Date: Jul 2007
Posts: 47
Reputation:
Solved Threads: 1
Hello everybody,
I wanted to ask Bob about this piece of code:
you said that this formula: 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.
I wanted to ask Bob about this piece of code:
C++ Syntax (Toggle Plain Text)
int main() { srand((unsigned)time(0)); int random_integer; int lowest=1, highest=10; int range=(highest-lowest)+1; for(int index=0; index<20; index++){ random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); cout << random_integer << endl; }
C++ Syntax (Toggle Plain Text)
random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0))
and I also wanted to ask how did you develope this formula(what concept is it based on)?
Thank you.
![]() |
Similar Threads
- Compile time errors in C++ while generating random numbers (C++)
- C++ Reorder random numbers (C++)
- random numbers all different??? (C++)
Other Threads in the C++ Forum
- Previous Thread: File read problem
- Next Thread: passing linked lists through functions??
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets




