Question

I need random symbols from the ASCII range

http://i.imgur.com/ZeNZa8b.jpg

but what I'm getting is some strange symbols. Why?

http://i.imgur.com/XxyfMV3.jpg

#include <iostream>
#include <stdio.h>
#include <time.h>
using namespace std;

int main()
{
    char k;
    int i;
    srand(time(0));
    for (i = 1; i < -10; i++)
    {
        k=(rand() % 33) + 15;
        printf("%c", k);
    }
    return 0;
}
Was it helpful?

Solution

Ok. You need to understand mod a little bit better: here you go

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
main()
{
    char k;
    int i;
    srand(time(NULL));
    for(i = 1; i <= 10; i++)
    {
            k=(rand() % 14) + 33; //range is (47 - 33), add 33 to get that range.
            printf("%c", k);
    }
    return 0;
}

The mod will take the number n, and place it between the range of 0-13, then we add 33 to put it between 33 and 47

Hope this helps.

What you did was take the number and place it between the range of 0 and 32, then add 15 placing it in the range of 15 and 47 (SI - '/')

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top