Random Number Generator with array

Pages: 12
How do I generate that random number as an index to access the ordered array?


The "index" is just the number you put in brackets to access an element.

1
2
3
4
cout << alpha[5];  // <- here, '5' is our index

int foo = 3;
cout << alpha[foo];  // <- here, 'foo' is our index (since foo==3, this means 3 is our index) 


If you want a random element, then you want a random index.

You already know how to make a random number -- you've done it previously. So all you have to do now is use that random number as an index, and voila, you'll get a random character.


Then, all you have to do is put that random character in a second array.
Hw do i put tht random character in a second array. Esp when nw i gt 2 random char.
Do you not know how to assign values to an array?

 
array_you_want_to_assign[ index_of_element_to_be_assigned ] = value_you_are_assigning;
Last edited on
I dono hw to assign 2 random values into positon 1,2,3,4...26. Somemore with a loop.
Please type out full words. It is hard to read your posts when you abbreviate everything.

If the overall process is too confusing, break it into smaller steps. If you don't know how to assign multiple variables, how about just 1? Can you assign one random character to a single variable?
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<cmath>
#include<ctime>
#include<stdlib.h>
using namespace std;

int main()
{
   unsigned seed = time (0);
   srand (seed);

   for (int t=0;t<2;t++)
   {
      int n = rand() % 26;
      char c = (char)(n+97); //ASCII
      cout << "------" << "\n";
      cout << c << "\n";
      cout << "n";
      cout << t+1;
      cout << " = ";
      cout << n << "\n";
      cout << "------" << "\n";
   }
}



z
n1 = 25
------
------
s
n2 = 18
------


b = 1
g = 6
c = 2
z = 25
k = 10
e = 4

b = 1
1 + 97 = 98 = b in ASCII

g = 6
6 + 97 = 103 = g in ASCII
refer to http://slayeroffice.com/tools/ascii/

create a simple random number gen. add 97 and you get the character.

Copying is about reverse-engineering
Last edited on
You know you can use character literals so you don't have to memorize ascii codes:

1
2
3
char c = (char)(n+97); //<-  what is 97?

char c = (char)(n + 'a'); // <- much more clear 



Though OP specifically asked about using a lookup table.
Like this?

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
#include<iostream>
#include<ctime>
using namespace std;

int main()
{
    int number[2], t, x;
    srand(time(0));
    char letter[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; 
    
    for(t=0;t<2;t++)
    {
        number[t] = rand() % 26;
    }
    for(t=0;t<2;t++)
    {
        x=number[t];
        cout << "" << "\n";
        cout << letter[x] << "\n";
        cout << "n";
        cout << t+1;
        cout << " = ";
        cout << x << "\n";
    }
}
Last edited on
Topic archived. No new replies allowed.
Pages: 12