Author Topic: Random in c++??  (Read 8915 times)

Cman

  • User
  • *
  • Posts: 9
Random in c++??
« on: April 14, 2005, 09:22:43 PM »
umm i am trying to write a simple program that randomly picks a letter from the alphabet.  but i realized that i dont know how to do random so i thought i would post and see if you could help.  (i think what im looking for is kindof a c equivelant to VBs randomize(and i dont know how that works, my freind is writing a game i VB))

Thank you
"yeah, i know basic BASIC"

Gillius

  • Administrator
  • User
  • *****
  • Posts: 147
    • http://www.gillius.org/
Random in c++??
« Reply #1 on: April 14, 2005, 09:41:16 PM »
Code: [Select]
char r = rand() % 26 + 'a'; is one very quick way of doing it.
Gillius
Gillius's Programming http://www.gillius.org/

Cman

  • User
  • *
  • Posts: 9
Random in c++??
« Reply #2 on: April 21, 2005, 06:10:44 PM »
thank you, but could you by any chance explain that code a bit more and maybe give an example?
yeah i just didn't really understand all the stuff after "rand()"
thank you
"yeah, i know basic BASIC"

Gillius

  • Administrator
  • User
  • *****
  • Posts: 147
    • http://www.gillius.org/
Random in c++??
« Reply #3 on: April 21, 2005, 09:22:59 PM »
% is the modulus operator (give you the remainder from a division).  'a' is the start of the alphabet, and there are 26 characters in the alphabet, so we add a number from 0 to 25 to 'a' to get 'a' through 'z'.  It's a very quick way and there are better (meaning more "random") ways, but this is the easiest to understand.

In order to take advantage of adding to 'a', I had to know about the ASCII character set.  You can see on a site like http://www.asciichart.com how the characters map to numbers.
Gillius
Gillius's Programming http://www.gillius.org/

Cman

  • User
  • *
  • Posts: 9
Random in c++??
« Reply #4 on: April 22, 2005, 03:05:26 PM »
this method when put into a very simple program:
 
Code: [Select]

char a = rand() % 26 + 'A';
cout << a <<endl;


this continually outputs P and when used with a do...while loop continually outputs a very repedetive and predictable sting of letters.

if maybe you could suggest one of those "more random" methods

that would be great thanks
"yeah, i know basic BASIC"

Gillius

  • Administrator
  • User
  • *****
  • Posts: 147
    • http://www.gillius.org/
Random in c++??
« Reply #5 on: April 22, 2005, 07:51:54 PM »
rand() will produce the same sequence every time unless you give it a seed.  There is no such thing ... well at least in code ... as a magical source of perfect randomness.  rand() is an algorithm that generates a seemingly random number from the previous number (the seed).  If you give srand() the same seed, it will cause rand() to always generate the same sequence.

One way that people use to start the seed is to take the seed from the current time:
Code: [Select]
srand( time(0) );
I believe that's how it goes.  You use srand() at the start of your program, once.  You should NOT use srand() every time since time(0) only changes once per second, and even when it does it changes only the lower bits, which will give you very similar seeds.
Gillius
Gillius's Programming http://www.gillius.org/