how to keep a randomized value from an array randomized?

hi! Let's jump straight to the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctime>
#include <string>
#include <sstream>

using namespace std;


int main(){
srand(time(NULL));

int abilitydamage[2]{0,rand()% 1100 +1};
int damage;


for (int tttt = 0; tttt < 3; tttt++){
    cout << "this is the loop timer: " << tttt << endl;
    damage = abilitydamage[1];
    cout << "This is a randomizer test: " << damage << endl;
}
}


I'm pulling a randomized number from inside the ability array and give damage the same value. But I want this value to keep randomizing, and instead it remains static. I also noticed while running it several times, that even the initial random value would consistently show the same value in a row, and slowly increase it over time as I kept reopening the program. How do I keep the value randomized, and stop this weird behavior too?
1. Pick a better variable name to loop with.
2. Arrays start at 1. Meaning, your array of size to can be accessed - abilitydamage[0-1]. So you want the loop to run on 0 and 1.

3.
I also noticed while running it several times, that even the initial random value would consistently show the same value in a row, and slowly increase it over time as I kept reopening the program.


Read here:
http://stackoverflow.com/questions/6668282/srandtimenull-generating-similar-results


srand is pretty shittie and you should not use it, there is a much better way of doings things now, using the random header <random> introduced in c++11 -
http://www.cplusplus.com/reference/random/

Watch this great video -
https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful


Here is what it should look like though in your code -
1
2
3
4
5
6
for(int i = 0; i < 2; i++) // loops twice
{
     cout << "this is the loop timer: " << i << endl;
     damage = abilitydamage[i];
     cout << "This is a randomizer test: " << damage << endl;
}
Last edited on
ok, looked up the video on <random>, and tried to run the example code, but I only get this error:

terminate called after throwing an instance of 'std::runtime_error'
what(): random_device::random_device(const std::string&)

I get the same error using any example with random_device. Why is that? How do I fix that?

And the original question still stands, how do I keep the value in an array randomizing each time it is called?
And the original question still stands, how do I keep the value in an array randomizing each time it is called?

Could you be more specific, I don't understand the question. Each time what is called? you're not calling anything in your code. Keep what the value? It is being kept in the code I gave you.
Here is an example (generating 10 numbers with both methods)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <random>
#include <ctime>

int main()
{
    //the old method
    srand(time(nullptr));
    for(int i = 0; i < 10; ++i)
    {
        int r = rand() % 10 + 1; //returns random numbers
        std::cout << r << " ";
    }
    std::cout << "\n\n";

    //the new method
    std::default_random_engine e;
    //e.seed(999); //you can manually seed
    std::uniform_int_distribution<int> d(1, 9); //range

    for(int i = 0; i < 10; ++i)
    {
        int r = d(e);
        std::cout << r << " ";
    }
    std::cout << "\n";

    return 0;
}
with seed numbers the program doesn't crash, but because of that the numbers are now even generated the same. I really need someone to tell me how to get random_device working, and then I will test if it will continually randomize my value in the array.

I'll say again what I want:

1
2
3
4
5
6
7
8
9
int abilitydamage[2]{0,rand()% 1100 +1}; //the goal is to get this value to randomize each time it is being called.
int damage;


for (int tttt = 0; tttt < 3; tttt++){
    cout << "this is the loop timer: " << tttt << endl;
    damage = abilitydamage[1]; //so that everytime the loop prints it out, instead of giving e.g: "203,203,203" or "380,380,380", it should give something like "730,482,1039".
    cout << "This is a randomizer test: " << damage << endl;
}
The reason the numbers are being repeated is because here - rand()% 1100 +1 is returning a number, and then you simply give that number to damage 3 times. You'll need to refresh it -

1
2
3
4
5
6
7
8
9
10
int abilitydamage[2]{0, rand() % 1100 + 1};
int damage;

	
for (int tttt = 0; tttt < 3; tttt++){
	cout << "this is the loop timer: " << tttt << endl;
	damage = abilitydamage[1];
	cout << "This is a randomizer test: " << damage << endl;
	abilitydamage[1] = rand() % 1100 + 1; // get a new random number
}
Last edited on
yup, that works! Ty so far!

I want to know one other thing, I have dozens of these randomized numbers in my array, is there a smooth way to let them re-roll all at once, or can I only change them one by one, like in the example?

I have been trying to use abilitydamage[10] = {//all the random numbers..} for that, but the compiler won't accept it. Is this idea even possible?

//edit: removed the {} entirely, and the compiler let's it at least run, but I don't see any changes. Ughhhhhhh!!
Last edited on
Hello again! I have been working to sorting this issue out on my own, and I got it solved.

First, I learned more about <random> and found out a way to use it without random_device and instead with the system clock. I have to store every value I generate to a designated name/location, but that came in useful later on:

1
2
3
4
5
6
7
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();

std::minstd_rand0 generator (seed);

std::uniform_int_distribution<int> D_RANDOM_VALUE(1,1100); //The distribute value

int RANDOM_VALUE = D_RANDOM_VALUE(generator); //The actual value 


The arrays refuse to generate random numbers and proved to be the biggest roadblock. So instead of hacking together a big code to work around it, I figured, since I already have all the values stored to specific names, I don't need to bother forcing them into an array, but instead just let the array point to their location. This was a major step to a complete solution:

int *RandomArray[1]{&RANDOM_VALUE};


However, there was one more thing missing. The values were randomly generated the first run, but then again, stayed the same. After a bit of tinkering, I realized that I had to re-generate the base values after every time they were called. In my game I used a specific loop to re-generate every ability again, which is highly pragmatic and performance-heavy, but until it becomes a real issue / I figure out how to do it more practical, it will suffice.

1
2
3
4
5
6
7
for (int tttt = 0; tttt < 3; tttt++){

    RANDOM_VALUE = D_RANDOM_VALUE(generator);
    damage = *RandomArray[0];
    cout << "This is the damage: " << damage << "\n" << endl;
    cout << "This is the random value: " << RANDOM_VALUE << "\n" << endl;
}


And this is how I solved it. The game works perfectly fine, and the random-generator has not failed thus far. Here is the complete testcode that sums the post up:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
#include <time.h>
#include <string>
#include <sstream>
#include <random>
#include <chrono>

unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();

std::minstd_rand0 generator (seed);

std::uniform_int_distribution<int> D_RANDOM_VALUE(1,1100); //The distribute value

int RANDOM_VALUE = D_RANDOM_VALUE(generator); //The actual value

int health = 2000;
int damage;
int *RandomArray[1]{&RANDOM_VALUE};

using namespace std;

int main(){




AGAIN: //DEBUG

for (int tttt = 0; tttt < 3; tttt++){

    RANDOM_VALUE = D_RANDOM_VALUE(generator);

    cout << "this is the loop timer: " << tttt << "\n" << endl;
    damage = *RandomArray[0];
    health = health - damage;
    cout << "This is the damage: " << damage << "\n" << endl;
    cout << "This is the random value: " << RANDOM_VALUE << "\n" << endl;
    cout << "This is the health: " << health << "\n" << endl;
    damage = 0;
    health = 2000;
system("pause");
system("cls");
}


goto AGAIN; //DEBUG

}


With this, I will mark this thread as solved. Hope this will answer the question for everyone who may look it up in the future. Hello from 2016 by the way!! Wuuuu!
Topic archived. No new replies allowed.