Hi..

I'm a beginner and just study programming.

I have to make some random number. I found many solution in this site, but does'nt seem to be like i want.

Please see this screen shot. I have to make some simple program like sudoku game. And must put random number at the beginning of the game. But how to make the random number do not same between each other?

i try using

srand((unsigned) time(0));
int cus_num=rand()%4,

but the random number generated sometime were repeated.

thanks for your help.

Recommended Answers

All 5 Replies

You could fill each box with unique integers, shuffle them around (while making sure the sudoku is still valid) and then erase a few of the numbers (making sure it's still possible to solve).

There's probably quite a few ways to manage this, and there's plenty of information on the net to help you.

one more thing, i'm not allowed to use array in this project because our lecturer did'nt teach us yet.

so i guess how to solve it by using only basic coding...

You can't use an array for a sudoku program? I would have thought that you would have been taught arrays before being told to make something like this.

Anyway, considering there's only 16 cells in the sudoku, just make 16 integers and continue from there.

It would become quite complex in testing a particular sudoku,for validity without using arrays.So i guess if you are allowed to use string, it would be the appropriate type, though it involves overheads of integer to char conversions.

I certainly hope you don't have an srand before each rand!
Because that's a problem!

srand((unsigned) time(0));  <-- Stick me at top of program ONLY

int cus_num=rand()%4,

What time is it now? What time is it 0.0001 seconds from now? How fast is your computer? 1.8Gig, 2.8Gig?

When you seed your RNG with the clock it has an initial degree of randomness, but if your program is in a loop crunching data and constantly reseeding it with a clock, its reseeding with the same time. So the same random numbers will be produced, until it crosses that second barrier to become a new seed!


The rule of thumb for using srand() is, at the top of your program.
And after keyboard input. The human delay will help alter the time!

rand() is a poor RNG to begin with!

In terms of trying to debug something that seems RNG related. Have only one seed at the top of your program and hard code it to a value, only for purposes of debugging. (Don't use time, use zero or something>) All the rands after that will be the same thus your problem is more repeatable!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.