Solved

Random Numbers

Posted on 1997-05-02
6
188 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now