6 Replies - 2691 Views - Last Post: 23 February 2011 - 09:23 PM Rate Topic: -----

#1 quantum1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 quantum1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.

Thanks for your time.
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 quantum1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#6 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1