randomizer

what is the best way to randomize letters and numbers.. both seperately of course.
closed account (S6k9GNh0)
Actually, if you can figure out how to do it on integers, you can do it to ASCII based characters because they are represented by a identifier or number.
I am not actually not too sure on a randomizer code. And with this i would like to put them into a vector. but as i used rand_Num[x]= rand() % 26; for randomizing
with
1
2
#include <cstdlib>
#include <iostream> 
are the libraries that I have and when i attempt to use the .length() function i receive an error:
request for member `length' in `rand_Num', which is of non-class type `int[26]'


my array that i have is int rand_Num[26]; and the rand_Num.length() gives me that error
Last edited on
Arrays are not classes, and therefore do not have member functions. You declared an array with 26 elements, so that's how many it will always have, you wouldn't need to use a length function when it has a constant size. However if you want this container to be scalable, the vector container is the one you should be using. It has the length() function you are looking for.

http://cplusplus.com/reference/stl/vector/
randomize numbers in array:

1
2
3
4
5
6
7
8
9
10
11
12

srand((unsigned)time(0));
int rand_NUM[99];
for(int i=0;i<100;i++)
{
rand_NUM[i] = (rand()%99)+1; // Creates array with 99 random integers
}
for(int k=0;k<100;k++)
{
cout<<rand_NUM[k]<<endl; // Displays Array
}


Randomize letters in a sentence:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <string>
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand(time(NULL));
    string stuff = "I am not a random string";
    cout << stuff << endl;
    random_shuffle(stuff.begin(), stuff.end());
    cout << stuff << endl;
    return 0;
}
What exactly is the string library, and if you could please, could you explain what line 14 is?
here is an update in what i'm working on.. but i do have a problem because i have no idea how to work it..

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
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
void PressAKeyToContinue()
  {
  int c;
  cout<< "\n\nPress a key to continue..." ;
  c = getch();
  if (c == 0 || c == 224) getch();
  }
int RandN()
{
    srand(time(NULL));
    typedef template  vector<int>numrand(26,0);//RIGHT HERE IS THE PROBELM
    for(int x=0; x<numrand.length();x++)
    {
            numrand[x]=rand() % 26;
            return(numrand[x]);
    }
}
int main()
{
    srand(time(NULL));
    int rand_Num[26];
    
    //double a=1.0, b=26.0;
    for(int x =0; x<26; x++)
    {
            rand_Num[x]= RandN();
    }
    for(int x=0; x<26; x++)
    {
            cout<<rand_Num[x]<<" ";
    }
    for(int x=0; x<26; x++)
    {
            for(int y =x+1; y<26; y++)
            {
                    if(rand_Num[x]>rand_Num[y])
                    {
                        int dummy=rand_Num[x];
                        rand_Num[x]=rand_Num[y];
                        rand_Num[y]=dummy;
                    }
            }
    }
    cout<<"\n\n";
    for(int x=0; x<26; x++)
    {
            cout<<rand_Num[x]<<" ";
    }
    
    PressAKeyToContinue();    
    return(0);
}



18 F:\Dev\main.cpp expected unqualified-id before "template"
18 F:\Dev\main.cpp expected `,' or `;' before "template"
19 F:\Dev\main.cpp `numrand' undeclared (first use this function)


is what the error thing said..
What exactly is that typedef supposed to be doing?
i thought that was supposed to be there, cuz i thought i saw it somewhere. what is the right way to declare a vector?
Remove the typedef and the template.
still an error
1
2
3
4
5
6
7
8
9
10
int RandN()
{
    srand(time(NULL));
    vector<int>numrand(26,0);
    for(int x=0; x<numrand.length();x++)
    {
            numrand[x]=rand() % 26;
            return(numrand[x]);
    }
}



15 F:\Dev\main.cpp `vector' undeclared (first use this function)
15 F:\Dev\main.cpp expected primary-expression before "int"
15 F:\Dev\main.cpp expected `;' before "int"
16 F:\Dev\main.cpp `numrand' undeclared (first use this function)


Where am i missing the ';' ?
You are simply missing the <vector> header.

Also, your return stmt is inside the loop body. You want it after the loop.
the vector header just goes
<vector><int>numrand(26,0); right?


Then since it is a vector, how am i sending back the values?

and what is 'stmt'?


No. You write #include <vector> at the top of your source.

"Stmt" is short for "statement".
yeah, i shoulda known,.. as i asked before, how would you put the return statement?

EDIT:
and this error occured
17 F:\Dev\main.cpp 'class std::vector<int, std::allocator<int> >' has no member named 'length'
fixed that problem with .size()
Last edited on
Topic archived. No new replies allowed.