Trick the randomizer in C - c

I want to get random numbers between 1 to 10.
It actually works, but when it's in a loop, I don't really get random numbers.
int randomNum;
srand ( (unsigned int)time(NULL) );
randomNum = rand() % 10;
I've been spending hours here and in google looking for a solution, but it looks like no one really solved it (or maybe I didn't search good enough).
The value we get from the randomizer depends on the seconds (not miliseconds or something else, like in other programming language) and that's why the numbers are not random.
In addition, I don't want to download a package for C because I run my code in the university labs, and they won't allow it.
Is there anyone with a creative solution for this problem? maybe some mathematic functions?

To illustrate Sidoh's answer.
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(int argc, char** argv)
{
int i;
srand ( (unsigned int)time(NULL) );
for (i = 0; i < 100; i++)
{
printf("%d ", 1 + (rand() % 10));
}
putchar('\n');
return 0;
}
This produced the following results for my one time seed using time( ).
7 10 2 4 4 4 2 1 7 7 10 4 3 10 2 9 6 9 2 9 7 10 4 1 1 8 2 4 8 1 2
4 2 3 9 5 8 1 7 4 9 8 10 1 8 1 1 5 1 4 5 7 3 9 10 3 6 1 9 3 4 10
8 5 2 7 2 2 9 10 5 9 8 4 1 7 7 2 3 7 5 8 6 10 8 5 4 3 7 2 8 2 1 7
7 5 5 10 6 5

Do not seed the random number generator more than once. Since your code probably runs all within the same second, every query to rand uses the same seed, so you'll get the same number every time.

Dave Newman provides a very good answer.
Alternatively, you could also try a pseudo random generator, for example
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
int a0; // this value will be our requirement
int mod = 11; //this is the limit (0 - mod-1), here 10
int a; // this stores the previous value of a0;
int i; // loop variable
int mul=25; //multiplicative factor
int add=3; // additive factor
int limit=100; // our limit
srand ( (unsigned int)time(NULL) ); // initialize the seed
a0 = rand() % mod;
for(i=0;i<limit;i++)
{
printf("%d\t",a0);
a = a0;
a0 = (a * mul + add) % mod;
}
putchar('\n');
return 0;
}
The output::
1st run::
2 10 4 3 1 8 0 6 7 9 2 10 4 3 1 8 0 6
7 9 2 10 4 3 1 8 0 6 7 9 2 10 4 3 1 8
0 6 7 9 2 10 4 3 1 8 0 6 7 9 2 10 4 3
1 8 0 6 7 9 2 10 4 3 1 8 0 6 7 9 2 10
4 3 1 8 0 6 7 9 2 10 4 3 1 8 0 6 7 9
2 10 4 3 1 8 0 6 7 9
2nd output::
9 2 10 4 3 1 8 0 6 7 9 2 10 4 3 1 8 0
6 7 9 2 10 4 3 1 8 0 6 7 9 2 10 4 3 1
8 0 6 7 9 2 10 4 3 1 8 0 6 7 9 2 10 4
3 1 8 0 6 7 9 2 10 4 3 1 8 0 6 7 9 2
10 4 3 1 8 0 6 7 9 2 10 4 3 1 8 0 6 7
9 2 10 4 3 1 8 0 6 7

Related

Running weighted quick union

So I have to take an ID array
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
And perform weighted quick union on it. I have to perform the operations 9-0, 3-4, 5-8, 7-2, 2-1, 5-7, 0-3, and 4-2. Here's what I did to the array for these operations:
9-0
0 1 2 3 4 5 6 7 8 9
9 1 2 3 4 5 6 7 8 9
3-4
0 1 2 3 4 5 6 7 8 9
9 1 2 3 3 5 6 7 8 9
5-8
0 1 2 3 4 5 6 7 8 9
9 1 2 3 3 5 6 7 5 9
7-2
0 1 2 3 4 5 6 7 8 9
9 1 7 3 3 5 6 7 5 9
2-1
0 1 2 3 4 5 6 7 8 9
9 7 7 3 3 5 6 7 5 9
5-7
0 1 2 3 4 5 6 7 8 9
9 7 7 3 3 7 6 7 5 9
0-3
0 1 2 3 4 5 6 7 8 9
9 7 7 3 3 7 6 7 5 3
4-2
0 1 2 3 4 5 6 7 8 9
9 7 7 3 3 7 6 3 5 9
The problem is that the ID array operations are different depending on if you're using quick find or quick union or weighted quick union. So would this be right for weighted quick union? Here's the code I'm using for weighted quick union:
public class WeightedQuickUnionUF
{
private int[] id; // parent link (site indexed)
private int[] sz; // size of component for roots (site indexed)
private int count; // number of components
public WeightedQuickUnionUF(int N)
{
count = N;
id = new int[N];
for (int i = 0; i < N; i++) id[i] = i;
sz = new int[N];
for (int i = 0; i < N; i++) sz[i] = 1;
}
public int count()
{ return count; }
public boolean connected(int p, int q)
{ return find(p) == find(q); }
private int find(int p)
{ // Follow links to find a root.
while (p != id[p]) p = id[p];
return p;
}
public void union(int p, int q)
{
int i = find(p);
int j = find(q);
if (i == j) return;
// Make smaller root point to larger one.
if (sz[i] < sz[j]) { id[i] = j; sz[j] += sz[i]; }
else { id[j] = i; sz[i] += sz[j]; }
count--;
}
}
The code for weighted quick union shows you how it works but basically it's a type of Union-find where you connect two trees together. With weighted quick union you always connected the smaller tree to the larger one. The ID array is a representation of a tree where there's numbers on the top row and bottom row. If the number on top matches the bottom then that number is a root in the forest but for example if the top number is a 9 and the bottom number is 0 then it means 9 is a child of 0. The ID array starts out with 9 single node trees and operations like 9-0 connects two trees together.

J: Coordinates with specific value

Let's say we have array
0 1 2 3 4 5 8 7 8 9
There are two indexes that have value 8:
(i.10) ([#~8={) 0 1 2 3 4 5 8 7 8 9
6 8
Is there any shorter way to get this result? May be some built-in verb.
But more important. What about higher dimensions?
Let's say we have matrix 5x4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
I want to find out what are coordinates with value 6.
I want to get result such (there are three coordinates):
4 1
3 2
2 3
It's pretty basic task and I think it should exist some simple solution.
The same in three dimensions?
Thank you
Using Sparse array functionality ($.) provides a very fast and lean solution that also works for multiple dimensions.
]a=: 5 ]\ 1 + i. 8
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
6 = a
0 0 0 0 0
0 0 0 0 1
0 0 0 1 0
0 0 1 0 0
4 $. $. 6 = a
1 4
2 3
3 2
Tacitly:
getCoords=: 4 $. $.
getCoords 6 = a ,: a
0 1 4
0 2 3
0 3 2
1 1 4
1 2 3
1 3 2
Verb indices I. almost does the job.
When you have a simple list, I.'s use is straightforward:
I. 8 = 0 1 2 3 4 5 8 7 8 9
6 8
For higher order matrices you can pair it with antibase #: to get the coordinates in base $ matrix. Eg:
]a =: 4 5 $ 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
I. 6 = ,a
9 13 17
($a) #: 9 13 17
1 4
2 3
3 2
Similarly, for any number of dimensions: flatten (,), compare (=), get indices (I.) and convert coordinates (($a)&#:):
]coords =: ($a) #: I. 5 = , a =: ? 5 6 7 $ 10
0 0 2
0 2 1
0 2 3
...
(<"1 coords) { a
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
By the way, you can write I. x = y as x (I.#:=) y for extra performance. It is special code for
indices where x f y

How to do print formatting in Python with chunks of strings?

I'm having some trouble with formatting the pyramid. I've tried to use format when printing from the loop but that didn't seem to work and just breaks the program. What would be different ways to format the output. The only trouble that I am having is when I am printing 10 and up when there's double digits. What would be the best approach formatting the printing output? I've tried variety of ways but couldn't make formatting work within the loop from documentation
https://docs.python.org/3.5/library/string.html#formatstrings
Here is the script:
userinput = int(input("Enter the number of lines: " )) # User input of the total number of lines
userinput = userinput + 1 # adding a value of 1 additionally with the user input to make numbers even
for i in range(1, userinput): # Loop through lines from 1 to userinput
for j in range(userinput - i): # printing spaces, 1 at a time from j = 1 to j = userinput - i
print(" ", end = " ")
for j in range(i, 0, -1): # printing number decreasing from the line number j to 1
print(j, end = " ")
for j in range(2,i + 1): # Printing number increasing from 2 to line number j
print(j, end = " ")
print()
j += 1
The output when its less than 10
Enter the number of lines: 9
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
The output when it's 15 or more:
Enter the number of lines: 15
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11
12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12
13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13
14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
When I have reserved an extra space for 10 and up, here is what my outout looks like: (The dots were used to distinguish from empty space, all I did was added a " " quotes in the beginning of the print.
Enter the number of lines: 12
. . . . . . . . . . . . 1
. . . . . . . . . . . 2 1 2
. . . . . . . . . . 3 2 1 2 3
. . . . . . . . . 4 3 2 1 2 3 4
. . . . . . . . 5 4 3 2 1 2 3 4 5
. . . . . . . 6 5 4 3 2 1 2 3 4 5 6
. . . . . . 7 6 5 4 3 2 1 2 3 4 5 6 7
. . . . . 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
. . . . 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
. . . 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
. . 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11
. 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12
Here is what I've tried changing by adding aditional space
for j in range(userinput - i): # printing spaces, 1 at a time from j = 1 to j = userinput - i
print(".", end = " ")
for j in range(i, 0, -1): # printing number decreasing from the line number j to 1
print(" ", j, end = "")
for j in range(2,i + 1): # Printing number increasing from 2 to line number j
print(" ", j, end = "")
for j in range(userinput - i): # printing spaces, 1 at a time from j = 1 to j = userinput - i
print(" ", end = " ")
Here is the ideal output of what I am trying to accomplish:
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11
12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12
13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13
14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Thank you!
The things to consider for this problem are
The length of the largest number.
The length of the current number being printed.
The difference in lengths.
In order to correctly space everything, you're going to need to print extra
spaces after the numbers with less digits (to compensate for the extra digits in the larger number).
For example, if you have a row that contains the number 10, in order to correctly space the other smaller numbers, you're going to need to use extra spaces to compensate for that second digit in the number 10.
This solution works for me.
userinput = int(input("Enter the number of lines: " ))
userinput = userinput + 1
# Here, you can see I am storing the length of the largest number
input_length = len(str(userinput))
for i in range(1, userinput):
# First the row is positioned as needed with the correct number of spaces
spaces = " " * input_length
for j in range(userinput - i):
print(spaces, end = " ")
for j in range(i, 0, -1):
# Now, the current numbers length is compared to the
# largest number's length, and the appropriate number
# of spaces are appended after the number.
spaces = " " * (input_length + 1 - len(str(j)))
print(j, end = spaces)
for j in range(2,i + 1):
# The same is done here as in the previous loop.
spaces = " " * (input_length + 1 - len(str(j)))
print(j, end = spaces)
print()
j += 1
Take a look at
https://stackoverflow.com/a/13077777/6510412
I think this might be what you're looking for. I hope it helps.

Tacit function to multiply five consecutive number in a list: J, j701

I'm working on Project Euler, I'm on problem 8, and I'm trying a simple brute force: Multiply each consecutive 5 digit of the number, make a list with the results, and find the higher.
This is the code I'm currently trying to write in J:
n =: 731671765313x
NB. 'n' will be the complete 1000-digits number
itl =: (".#;"0#":)
NB. 'itl' transform an integer in a list of his digit
N =: itl n
NB. just for short writing
takeFive =: 5 {. ] }.~ 1 -~ [
NB. this is a dyad, I get this code thanks to '13 : '5{.(x-1)}.y'
NB. that take a starting index and it's applied to a list
How I can use takeFive for all the index of N?
I tried:
(i.#N) takeFive N
|length error: takeFive
| (i.#N) takeFive N
but it doesn't work and I don't know why.
Thank you all.
1. The reason that (i.#N) takeFive N is not working is that you are essentially trying to run 5{. ((i.#N)-1) }. Nbut you have to use x not as a list but as an atom. You can do that by setting the appropriate left-right rank " of the verb:
(i.#N) (takeFive"0 _) N
7 3 1 6 7
7 3 1 6 7
3 1 6 7 1
1 6 7 1 7
6 7 1 7 6
7 1 7 6 5
1 7 6 5 3
7 6 5 3 1
6 5 3 1 3
5 3 1 3 0
3 1 3 0 0
1 3 0 0 0
2. One other way is to bind (&) your list (N) to takeFive and then run the binded-verb through every i.#N. To do this, it's better to use the reverse version of takeFive: takeFive~:
((N&(takeFive~))"0) i.#N
7 3 1 6 7
7 3 1 6 7
3 1 6 7 1
1 6 7 1 7
6 7 1 7 6
7 1 7 6 5
1 7 6 5 3
7 6 5 3 1
6 5 3 1 3
5 3 1 3 0
3 1 3 0 0
1 3 0 0 0
or (N&(takeFive~)) each i.#N.
3. I think, though, that the infix dyad \ might serve you better:
5 >\N
7 3 1 6 7
3 1 6 7 1
1 6 7 1 7
6 7 1 7 6
7 1 7 6 5
1 7 6 5 3
7 6 5 3 1
6 5 3 1 3

Index manipulation of array in C

Begining with an ordered array
[1, 2, 3, 4, 5, 6, 8, 9, 10]
How would be the way to get every iteration the following results?
1 2 3 4 5 6 7 8 9 10
1 3 4 5 6 7 8 9 10 2
1 4 5 6 7 8 9 10 2 3
1 5 6 7 8 9 10 2 3 4
1 6 7 8 9 10 2 3 4 5
1 7 8 9 10 2 3 4 5 6
1 8 9 10 2 3 4 5 6 7
1 9 10 2 3 4 5 6 7 8
1 10 2 3 4 5 6 7 8 9
#include <stdio.h>
#define MAX 10
int a[MAX], i,j,cnt=2;
main (){
for (i=0; i<MAX; i++){
a[i]= i+1;
}
for (i=0; i<MAX; i++) {
printf ("%d ", a[i]);
}
printf ("\n");
for (j=0; j < MAX-2;j++){
a[0]=1;
for (i=1; i < MAX-1; i++){
if (a[i]%MAX != 0){
a[i]= a[i] + 1;
}else{
if (a[i]==10) {
//printf ("a[%d]: %d \t ** %d\n", i , a[i] ,cnt);
//a[i-1]= i;
a[i] = cnt;
}
}
}
for (i=0; i<MAX; i++) {
printf ("%d ", a[i]);
}
printf ("\n");
}
}
Now I almost get it but the last column is not right, What should I do?
1 2 3 4 5 6 7 8 9 10
1 3 4 5 6 7 8 9 10 10
1 4 5 6 7 8 9 10 2 10
1 5 6 7 8 9 10 2 3 10
1 6 7 8 9 10 2 3 4 10
1 7 8 9 10 2 3 4 5 10
1 8 9 10 2 3 4 5 6 10
1 9 10 2 3 4 5 6 7 10
1 10 2 3 4 5 6 7 8 10
C arrays are indexed from 0. So when you access elements from 1 to MAX, you are running off the end of the array.
Have your loops go from 0 to MAX-1. Customary way to write it is
for (i=0 ; i < MAX ; ++i)
...so anybody reading your code can immediately prove that the array index never equals MAX.
Well, at a minimum, arrays in C are zero based so you are writing past the end of the array. For an array declared int foo[MAX] valid elements are from foo[0]…foo[MAX-1]
Specifically a[MAX] might well reference the memory location that the variable i uses, causing the loop to reset when it attempt to overwrite a[MAX].
Either shift everything down by one, or declare your array MAX+1 and ignore the zero bit.
Oh, and you should not need to set a[1]=1; every time.

Resources