random byte 0-6

I want to randomly pick a byte from 0 to 6 not sure how to do this.
WackoMikeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
You could use

#include <stdlib.h>
#include <stdio.h>
#include <time.h>


typedef unsigned char BYTE;

BYTE RandomByteFrom0To6() {

    int i = rand() * 6 / RAND_MAX; // create a random number and map it to the range 0-6

    return (BYTE) i;
}

and call it like

int main( void )
{
   BYTE i;

   /* Seed the random-number generator with current time so that
    * the numbers will be different every time we run.
    */
   srand( (unsigned)time( NULL ) );

   /* Display 10 numbers. */
   for( i = 0;   i < 10;i++ )
      printf( "  %1.1d\n", RandomByteFrom0To6() );

   return 0;
}
0
Jaime OlivaresSoftware ArchitectCommented:

int main()
{
   int randomnum;
 
   srand(time(NULL));

   // Do work here that uses rand(), for example
   for (int i= 0; i< 10; i++)
   {
     randomnum = rand() % 7;
     printf("%i\n\r", randomnum);
   }

   return 0;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jaime OlivaresSoftware ArchitectCommented:
oops! a minute later.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

NievergeltSenior SW DevCommented:
Yes jaime, but your example should work.
0
Jaime OlivaresSoftware ArchitectCommented:
You will need some headers, here is a fully tested code:

#include <stdio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>

int main()
{
   int randomnum;
 
   srand(time(NULL));

   // Do work here that uses rand(), for example
   for (int i= 0; i< 10; i++)
   {
     randomnum = rand() % 7;
     printf("%i\n\r", randomnum);
   }

   return 0;
}
0
jkrCommented:
>> Yes jaime, but your example should work.

Both will work.
0
WackoMikeAuthor Commented:
since you both answer so quick i gave you both 250 i hope that is ok with you guys. Thanks for the help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.