Help Sought

Hello all,

I am new to C++ language. I needed an output of 5 random numbers each for X and Y. The conditions for the random numbers are a) the numbers should be greater than 50.
b) each number should have a minimum absolute distance of atleast 20 from its previous values. to be precise, i needed to get a set of numbers like 51,71,95,117,140.

The following code is satisfying the first condition but not the second one. Please help !!

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
  #include <iostream>
#include <iomanip> // for setprecision
#include <stdio.h>
#include <stdlib.h>     /* srand, rand */
#include <time.h>
#include <string.h>    /* time */

using namespace std;
int xnumber,ynumber;
bool isInside (int xnumber,int ynumber);
bool isClose (int xnumber, int ynumber);
bool fStuckinLoop;
int maxloops = 1000;
int loopcount;
int** initialPositions;
int tr,nTrials;
int NOBJECTSMAX = 5;


int main()
{
    srand((int)time(NULL));
    loopcount = 0;
    fStuckinLoop = 1;
    initialPositions = new int*[nTrials];



    bool yesInside,yesClose,yesCloseX,yesCloseY;

         initialPositions[tr] = new int[NOBJECTSMAX];

         memset(initialPositions[tr],0,2*NOBJECTSMAX*sizeof(int));






        for (int i = 0; i<NOBJECTSMAX;i++)
        {


            yesClose = false;
            yesInside = false;







           do
            {
            xnumber = (rand()%200)+1;
            ynumber = (rand()%200)+1;
            yesInside = isInside (xnumber,ynumber);
            yesClose = isClose (xnumber, ynumber);
            } while ((!yesInside)|| (!yesClose));
















            initialPositions[tr][i*2+0] = xnumber;
            initialPositions[tr][i*2+1] = ynumber;










            cout << "Number ---  " << initialPositions[tr][i*2+0]  << "---" << initialPositions[tr][i*2+1] <<"    Bool 1 ---" << yesInside << "    Bool 2 ---" << yesClose << endl;





            }













    return 0;
}




bool isInside (int xnumber,int ynumber)
{
    bool yesDivtwo;
    yesDivtwo = false;

    if ((xnumber>100) && (ynumber>100))

        yesDivtwo = true;

        return yesDivtwo;

}



bool isClose (int xnumber, int ynumber)

{

    bool isCloseflag;
    isCloseflag = false;

    int DistanceX,DistanceY;
            for (int i = 0; i<NOBJECTSMAX; i++)
            {


            DistanceX = abs(xnumber-initialPositions[tr][(i)*2+0]);
            DistanceY = abs(ynumber-initialPositions[tr][(i)*2+1]);



            if ((DistanceX>20) && (DistanceY >20))

                isCloseflag = true;
            else
                isCloseflag = false;
                break;



            }
    return isCloseflag;
}

Last edited on
Consider this:
1. At start Base = 51
2. You pick a random number A that is >= 0
3. You have found B = Base + A
4. Base = B + 20
5. Repeat from step 2 for the next B

How about int nextValue( int & Base ); that returns B and updates Base?


Edit. \emph{randomness}
Last edited on
Thank you. I just gave that # sequence as an example. I need random #s that should be greater than 50 and differ by 20 from other values.
closed account (E0p9LyTq)
Using the C++ random library:
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
#include <iostream>
#include <random>
#include <chrono>

int main()
{
   // obtain a seed from the system clock
   unsigned int seed = std::chrono::system_clock::now().time_since_epoch().count();

   // create a random number generator
   std::mt19937 gen(seed);
   
   // discard the first 10,240 random numbers
   constexpr static std::size_t NUM_DISCARD = 10240;
   gen.discard(NUM_DISCARD);
   
   // set a distribution range (1 - 200)
   std::uniform_int_distribution<int> dist(1, 200);

   // set the number of random numbers to create
   const int randNumbers = 5;
   
   // stores the last number
   int lastNumber = 50;
   
   // obtain and display random numbers
   for (int i = 0; i < randNumbers; i++)
   {
      while (true)
      {
         // pick a random number within the distribution (1 - 200)
         int numPicked = dist(gen);

         // only select a number greater than 20
         if (numPicked > 20)
         {
            lastNumber += numPicked;
            std::cout << lastNumber << " ";
            break;
         }
      }
   }
   std::cout << "\n";
}


134 245 285 473 498
@FurryGuy:

1. The while-loop+if>20 is unnecessary work; one can calculate the new value to be "exactly at least" 20 larger than the previous.
2. You increment lastNumber by picked-number. That becomes way more than "at least 20".

SriRam123 wrote:
I need random #s that should be greater than 50 and differ by 20 from other values.

Exactly what my algorithm yields.

Do you want them in ascending order, or would it be more fun to shuffle?
Last edited on
closed account (E0p9LyTq)
1. The while-loop+if>20 is unnecessary work; one can calculate the new value to be "exactly at least" 20 larger than the previous.
2. You increment lastNumber by picked-number. That becomes way more than "at least 20".

I wrote the code the way I did because of the OP's requirement:
b) each number should have a minimum absolute distance of atleast 20 from its previous values.

Not just 20, at LEAST 20.

Yes, I could have made the uniform integer distribution to have a minimum value of 20, that would have been a better way to write the code. I blame my less than perfect effort on a quick code slam, something that makes it easier for someone using rand/srand and unfamiliar with the C++ <random> library to understand.

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
#include <iostream>
#include <random>
#include <chrono>

int main()
{
   // obtain a seed from the system clock
   unsigned int seed = std::chrono::system_clock::now().time_since_epoch().count();

   // create a random number generator
   std::mt19937 gen(seed);

   // discard the first 10,240 random numbers
   constexpr static std::size_t NUM_DISCARD = 10240;
   gen.discard(NUM_DISCARD);

   // set a distribution range (20 - 200)
   std::uniform_int_distribution<int> dist(20, 200);

   // set the number of random numbers to create
   const int randNumbers = 5;

   // stores the chosen number, initializing to a value that will produce an
   // initial value of at least 51
   int pickedNumber = 31;

   // obtain and display random numbers
   for (int i = 0; i < randNumbers; i++)
   {
         // pick the random number
         pickedNumber += dist(gen);
         std::cout << pickedNumber << " ";
   }
   std::cout << "\n";
}
Last edited on
closed account (E0p9LyTq)
What does mt19937 mean?

*sigh*

If you would ever bother to actually look at what is contained in the <random> library you'd find out it is the name of ONE of the predefined random number generators, using the Marsenne twister algorithm. mt19937 is a 32-bit Mersenne Twister by Matsumoto and Nishimura, 1998.

http://en.cppreference.com/w/cpp/header/random
Last edited on
> What does mt19937 mean?

mt - pseudo random number generator using the Mersenne Twister algorithm

19937 - based on the Mersenne prime 219937−1.

Ergo mt19937


> Can't they find a more standard name for it

MT19937 is a standard name that has been in wide use for more than a decade.
https://gist.github.com/mstum/8367363
Amazing! You were created by a trash can right?

It is obvious to everyone you are trolling the site. The crap code you post shows a rather competent understanding of the language to make up that bullshit and yet you ask questions that make you look like you have less knowledge than that of a day one beginner. I don't see how admin hasn't banned you again already.
Topic archived. No new replies allowed.