# Random Numbers

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 12-23-2002
Korhedron
Random Numbers
How do I get random values, eg. between 1 and 10 ?

Also, can I write :

Code:

```if (name=="me") { int age == age + 2 }```
or something of the sort ? I have an integer which already has a value, and I want to add a modifier if certain things occur, in this case that name is "me".

Thanks !
• 12-23-2002
alpha
Code:

```//it would be like this int age; if(name == "me") {     age += 2; }```
• 12-23-2002
Korhedron
Code:

```int age; if(name=="me") { age += 2; } // That would work ? Can I also do { age -= 2; } // ? Thanks...```
And can you tell me how to get random numbers ? Thanks a lot for your help.
• 12-23-2002
alpha
yes you can do:
age -= 2;

for random numbers:

http://www.cprogramming.com/boardfaq.html#random

there are also a number of threads covering this topic, do a search, you'll be able to find the answer to your question.
• 12-23-2002
Korhedron
Code:

```#include <stdlib.h> //for rand #include <stdio.h> //for printf #include <time.h>  //for time int main(void) {     srand(time(NULL));     printf("A random number from 0 to 99: %d", rand() % 100);     return 0; }```
What is srand(time(NULL)); ?
What's %d in the printf section ?

Also,
Code:

```int rand_mid(int low, int high) {   return low+rand()%(high-low+1); }```
If I used this, I'd write something like :

cout << rand_min(1, 10)

to get random numbers between 1 and 10 ?
• 12-23-2002
alpha
sure...haven't tried, but it should work. assuming it is in a member function in the struct.

edit:: I'd like to know if it works.
• 12-23-2002
Korhedron
Sorry, I was modifying my last post when you posted your reply... I'll repeat it :

Code:

```int rand_mid(int low, int high) {   return low+rand()%(high-low+1); }```

If I used this, I'd write something like :

cout << rand_min(1, 10)

to get random numbers between 1 and 10 ?
• 12-23-2002
srand(time(NULL)) is the seed for the random number generator. Without your random number sequence is likely to be the same sequence each time you run the program.

%d is C syntax for "insert an integer here" in the input/output with the integer to insert being the value after the comma.

return low + (rand() % (high - low + 1));

should work for a random number between high and low, inclusive, but only a run of the code will prove it.
• 12-23-2002
Korhedron
Can I use something else than %d ? Something C++...
• 12-23-2002
printf("A random number from 0 to 99: %d", rand() % 100);

becomes:

cout << "A random number from 0 to 99 " << rand_mid(low, high);

Need to use this line:

#include <iostream.h>

or these

#include <iostream>

using namespace std;
OR
using std::cout;
OR
std::cout << whatever; every time you use cout.
• 12-23-2002
subdene
How come no-one seems to use randomize() and random() these days from stdlib.h. Are they just not part of the standard and are therefore just not used?
• 12-23-2002
Prelude
>How come no-one seems to use randomize() and random() these days from stdlib.h.
Because random and randomize are specialized compiler extensions, and therefore nonstandard.

-Prelude
• 12-23-2002
vasanth
what ever you do.. computer generated numbers are not truly random.. The algorithm may be complex but still the number is not eandom.. To truly generate a random number you have to take input from the environment such as noise, temp etc etc etc and then using some algorithm convert those into a number......
• 12-23-2002
CodeMonkey
lol, in that case, nothing is truly "random" because there is a logical cause for every event in this universe, saying one can manipulate lone molecules.
• 12-24-2002
Korhedron
Thanks a lot...

About rand_mid(low,high), I can use anything as rand_mid, or does it have to be called rand_mid ? Can I use random(low,high) ?
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last