Solved

Random Numbers

Posted on 1997-05-02
6
196 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

776 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