Solved

Random Numbers

Posted on 1997-05-02
6
197 Views
Last Modified: 2010-04-15
I am trying to write a program which will print out a different random number each time it is executed.

Here is what I have so far:

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

void main()
{
unsigned int number;
time_t t;

      srand((unsigned)time(&t));
      number = rand() % 100;
      printf("%d", number);
}

Unfortunately this question has the effect of only changing the random number once every second, and so if this program is run five times within one second the same number is generated.
If somebody could suggest a way of writing this program so that a different number is generated EVERY time it is run.
(intended platform is Unix)
0
Comment
Question by:idek1
  • 3
  • 2
6 Comments
 
LVL 5

Accepted Solution

by:
yonat earned 50 total points
ID: 1250232
There are several pseudo-random entities you can use:
- ticks since startup
- current process ID
- current process base memory address
- current thread ID

If you have a windows system, you can also use things like:
- current window handle
- current mouse position

0
 

Author Comment

by:idek1
ID: 1250233
The ideas of using process ID etc. are quite good, however, having only recently been enlightened to Unix, I am not confident  that I know the right functions to use to get this information.
How would I, for example, obtain the current process number, and would this number change if the program was run 5 times in a row in a very short time? ( < 1 sec)


0
 
LVL 5

Expert Comment

by:yonat
ID: 1250234
>  the right functions to use to get this information

I am not a Uniz wiz myself, so I can't help you there - sorry.

> would [the process ID] change if the program was run 5 times in
> a row in a very short time

Not necessarily. This is why you need to use a combination of
pseudo-random entities: The ticks since startup *would* change in
this case.
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:idek1
ID: 1250235
Anybody else know how to get the ticks since startup in Unix?
:)

0
 
LVL 5

Expert Comment

by:yonat
ID: 1250236
You could try asking this in the Unix Programming area. They'll
probably know.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1250237
You might see if gettimeofday on your system can give you sub-second times,
but something like
  srand((unsigned)time(0)*997+getpid()*991);
is probaly good enough. I doubt you can wrap the pid in under a second,
and even if you did, the rand() % 100 is probably the weak link anyway.
I'd change to random and srandom before trying to improve the seed furthur
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question