# Manipulate the random number generator to generate random characters b

Page 1 of 1

## 6 Replies - 5487 Views - Last Post: 23 February 2011 - 09:23 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=218350&amp;s=ed2970aa925687402a096ca85f9af673&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 quantum1

Reputation: 0
• Posts: 8
• Joined: 23-February 11

# Manipulate the random number generator to generate random characters b

Posted 23 February 2011 - 07:43 PM

How can I manipulate the random number generator to generate random characters between a range of ASCII codes using C++?
Is This A Good Question/Topic? 0

## Replies To: Manipulate the random number generator to generate random characters b

### #2 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Manipulate the random number generator to generate random characters b

Posted 23 February 2011 - 08:23 PM

Generate random numbers between 0 and 25, then (for uppercase letters) add 65 or (for lowercase letters) add 97, and assign the result to a char variable.

### #3 quantum1

Reputation: 0
• Posts: 8
• Joined: 23-February 11

## Re: Manipulate the random number generator to generate random characters b

Posted 23 February 2011 - 08:31 PM

It has to be eight characters long and in ASCII code range 33-126.

### #4 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Manipulate the random number generator to generate random characters b

Posted 23 February 2011 - 08:36 PM

33-126 is 94 unique values, so generate random numbers in the range of 0-93, and add 33 to the result.

Put that in a loop that iterates 8 times (i = 0 to 7), assigning each result to the i-th element of an array.

### #5 quantum1

Reputation: 0
• Posts: 8
• Joined: 23-February 11

## Re: Manipulate the random number generator to generate random characters b

Posted 23 February 2011 - 08:52 PM

So far I have:

```randomCharacters = 33 + rand() % 93;

```

But I am not allowed to use loops yet. Is there any other way to make it generate 8 characters long?

### #6 Martyn.Rae

• The programming dinosaur

Reputation: 545
• Posts: 1,420
• Joined: 22-August 09

## Re: Manipulate the random number generator to generate random characters b

Posted 23 February 2011 - 09:01 PM

If you are not allowed to use loops, then you will need eight statements where each statement allocates to the next element in the result array.

### #7 r.stiltskin

• D.I.C Lover

Reputation: 2030
• Posts: 5,430
• Joined: 27-December 05

## Re: Manipulate the random number generator to generate random characters b

Posted 23 February 2011 - 09:23 PM

... and you need rand % 94 to get values 0 - 93.